Top Picks

   
January 23, 2013

 

Copying from asset to sdcard open failed: ENOENT (No such file or directory)

 

I am try to copy from asset to sdcard
Here is part of my code

private void CopyAssets(String folder) { File folders = new File(Environment.getExternalStorageDirectory().toString()+"/beatscache/"+folder); folders.mkdirs(); AssetManager assetManager = getAssets(); String[] files = null; try { files = assetManager.list(folder); } catch (IOException e) { Log.e("tag", e.getMessage()); } for(String filename : files) { InputStream in = null; OutputStream out = null; try { in = assetManager.open(folder+"/"+filename); // if files resides inside the "Files" directory itself out = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/"+"beatscache"+"/"+folder +"/" + filename); copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch(Exception e) { Log.e("tag", e.getMessage()); } } } 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); } }

And

CopyAssets("bin");

Log shows

E/tag(3801): /mnt/sdcard/beatscache/bin/alsa_ctl: open failed: ENOENT
(No such file or directory)

I have given READ and WRITE permissions in AndroidManifest.xml
and checked Environment.MEDIA_MOUNTED.equals(state) returns true.

Can you tell where I am doing wrong.?

 

Answer

Check if file is exists or not first then move your content there

final File dir = new File(cEnvironment.getExternalStorageDirectory().toString()+"/"+"beatscache"+"/"+folder +"/"); if(dir.exists()==false) { dir.mkdirs(); //create folders where write files final File file = new File(dir, filename); }
Professional Hosting fro Just Host

Filed under: Android Coding FAQ

Tags:

Comments

No Comments

Leave a reply