0

I'm using this code for coping file from asset folder into sdcard but I got error and files copies with volume 3KB. I spend about 3 hour for solving problem but I could not! What's problem and solution

This code is for Copy

FileOperations.copyFromAsset(MainActivity.this,"database_tafsir_persian_azim","quran/data/database_tafsir_persian_azim");

public static void copyFromAsset(Context context,String src,String dst) throws IOException {  


   try{
        AssetManager asset=context.getAssets();
        InputStream in = asset.open(src);
        OutputStream out = new FileOutputStream(new File(dst));//ERROR IS FROM THIS LINE
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
            Log.i("Copying", "please wait...");
        }
        in.close();   
        out.close();
        out.flush();
       }catch(Exception e){
           e.printStackTrace();
       }
   }

LOG CAT:

09-18 16:05:20.096: W/System.err(4219): java.io.FileNotFoundException: /quran/data/database_tafsir_persian_azim: open failed: ENOENT (No such file or directory)
09-18 16:05:20.165: D/TextLayoutCache(4219): Using debug level: 0 - Debug Enabled: 0
09-18 16:05:20.175: W/System.err(4219):     at libcore.io.IoBridge.open(IoBridge.java:419)
09-18 16:05:20.175: W/System.err(4219):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
09-18 16:05:20.175: W/System.err(4219):     at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
09-18 16:05:20.175: W/System.err(4219):     at ir.aiga.apps.quran.classes.FileOperations.copyFromAsset(FileOperations.java:112)
09-18 16:05:20.175: W/System.err(4219):     at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:732)
09-18 16:05:20.175: W/System.err(4219):     at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:1)
09-18 16:05:20.175: W/System.err(4219):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-18 16:05:20.175: W/System.err(4219):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-18 16:05:20.175: W/System.err(4219):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-18 16:05:20.186: W/System.err(4219):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-18 16:05:20.186: W/System.err(4219):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-18 16:05:20.186: W/System.err(4219):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-18 16:05:20.186: W/System.err(4219):     at java.lang.Thread.run(Thread.java:856)
09-18 16:05:20.186: W/System.err(4219): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
09-18 16:05:20.186: W/System.err(4219):     at libcore.io.Posix.open(Native Method)
09-18 16:05:20.186: W/System.err(4219):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-18 16:05:20.186: W/System.err(4219):     at libcore.io.IoBridge.open(IoBridge.java:403)
sdaasd
  • 13
  • 6

1 Answers1

0

Try creating the parent directories first, perhaps they don't exist:

File destination = new File(dst);
File parent = new File(destination.getParent());
parent.mkdirs();
OutputStream out = new FileOutputStream(destination);

Also make sure that dst is a correct path to your external directory (sd card). You should get that path with:

Environment.getExternalStorageDirectory().getPath() + "/your_file"; 
Simas
  • 43,548
  • 10
  • 88
  • 116
  • File destination = new File(dst); destination.getParent().mkdirs()////////////The method mkdirs() is undefined for the type String – sdaasd Sep 18 '14 at 11:44
  • My file has not dot in file name! file name is database_ayat – sdaasd Sep 18 '14 at 11:45
  • Sorry, I forgot to add Environment.getExternalStorageDirectory().getPath() or "/sdcard/" in destination file path – sdaasd Sep 18 '14 at 11:53