MCanSener
MCanSener

Reputation: 53

Copy file to sdcard

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..

Upvotes: 0

Views: 396

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

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

Upvotes: 1

Related Questions