0

I am trying to copy an image from the asset folder to the sdcard but doesn't seem to copy it on first launch. It creates the folder okay but doesn't copy the file over.

prefs = getPreferences(Context.MODE_PRIVATE);
    if (prefs.getBoolean("firstLaunch", true)) {
        prefs.edit().putBoolean("firstLaunch", false).commit();
    File nfile=new File(Environment.getExternalStorageDirectory()+"/My Images");
    nfile.mkdir();
    }
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("middle.jpg");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for(String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
          in = assetManager.open(filename);
          File outFile = new File(Environment.getExternalStorageDirectory()+ "/My Images" + filename);
          out = new FileOutputStream(outFile);
          copyFile(in, out);
        } catch(IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }     
        finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {

                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {

                }
            }
        }  
    }

   private void copyFile(InputStream in, OutputStream out) {
    // TODO Auto-generated method stub

}

middle.jpg is the file i want to copy over. Can any one tell me what i am doing wrong?

PS i have WRITE_EXTERNAL_STORAGE in my manifest.

Thanks

Allrounder
  • 685
  • 2
  • 9
  • 20

1 Answers1

0

You forgot to add / in the end of /My images while constructing the path

File outFile = new File(Environment.getExternalStorageDirectory()+ "/My Images/" + filename); out = new FileOutputStream(outFile);

because the filename would be MyImages+Filename so it wouldn't exists for copying.