Question

I have an android application which copies a file to sdcard.

My code block is like below:

private void CopyXmlFile2SdCard() {
    AssetManager asstMan = getAssets();
    try {
        InputStream in = null;
        OutputStream out = null;
        in = asstMan.open("platform.xml");
        File outFile = new File(getExternalFilesDir(null), "platform.xml");
        out = new FileOutputStream(outFile);
        CopyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;

    } catch (IOException e) {
        Log.e("Copy Error", "Failed to get asset file");
    }

}

My copy method is:

private void CopyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

I am having an error "File not found" on the line :

out = new FileOutputStream(outFile);

I also have given the permission required to copy to sdcard (write_external_storage). Any suggestions would be appreciated..

Was it helpful?

Solution

getExternalFilesDir is available from API LEVEL 8. Change it with Environment.getExternalStorageDirectory().

Edit.

Also, please be sure to add an sdcard in your device/emulator

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top