Domanda

GUYS

I'm new to android and this is my first post in StackOverflow.English as my second language,I'm not that good at it.I just looked for some answers here before,Now I think it's time for me to get involved in it.

There have been a problem occured when I try to copy the system log which located in /dev/log/* to my SD card.After some search on the answers here,I came across Copy file (image) from CacheDir to SD Card.So I had my code below:

private final String srcLocation = "/dev/log/radio";
private final String desLocation = "/mnt/sdcard/radio";

FileInputStream src;
FileOutputStream dst;
FileChannel mFCsrc;
FileChannel mFCdst;

public boolean copyFile(String sourceLocation, String destLocation) throws IOException {
          try {
                File sd = Environment.getExternalStorageDirectory();
                if(sd.canWrite()){
                    File source=new File(sourceLocation);
                    File dest=new File(destLocation);
                    if(!dest.exists()){
                        dest.createNewFile();
                    }
                    if(source.exists()){
                        src = new FileInputStream(source);
                        dst = new FileOutputStream(dest);
                        mFCsrc = src.getChannel();
                        mFCdst = dst.getChannel();
                        mFCsrc.transferTo(0, mFCsrc.size(), mFCdst);
                    }
                }
                return true;
            } catch (Exception ex) {
                ex.printStackTrace();
                return false;
            } finally {
                if (mFCsrc != null) {
                    mFCsrc.close();
                }
                if (mFCdst != null) {
                    mFCdst.close();
                }
            }
    }

I do have the file in my SD card which I can see it from my DDMS window,but it's size is 0.So,anyone gets a clue? Thanks in advance.(I try to give you a picture of my DDMS window,but since my reputation is not enough,I cann't use a picture.I'm sorry about that!!)

È stato utile?

Soluzione

You should debug. Step trough your code with the debugger and check what is happening.

Random thing that might be happening, but we have no way of knowing for sure: If "source" doesn't exist, you will create dest, but because source.exists() will return false,you don't do anything after that. You'll end up with the current behaviour, a newly created file without contents.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top