将文件从资产复制到android中的SD卡

时间:2014-09-18 11:37:42

标签: android

我正在使用此代码将资产文件夹中的文件复制到SD卡中,但我收到错误并且文件副本的卷为3KB。我花了大约3个小时来解决问题,但我不能! 有什么问题和解决方案

此代码适用于复制

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)

1 个答案:

答案 0 :(得分:0)

首先尝试创建父目录,也许它们不存在:

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

还要确保dst是外部目录(SD卡)的正确路径。你应该通过以下方式获得这条道路:

Environment.getExternalStorageDirectory().getPath() + "/your_file";