【问题标题】:Copying file from asset into sd card in android将文件从资产复制到android中的sd卡
【发布时间】:2014-09-18 11:37:41
【问题描述】:

我正在使用此代码将文件从资产文件夹复制到 sdcard,但出现错误并且文件副本的容量为 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();
       }
   }

日志猫:

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)

【问题讨论】:

标签: android


【解决方案1】:

先尝试创建父目录,可能它们不存在:

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"; 

【讨论】:

  • 文件目的地=新文件(dst); destination.getParent().mkdirs()//////////////////String类型的方法mkdirs()未定义
  • 我的文件的文件名中没有点!文件名为 database_ayat
  • 抱歉,我忘记在目标文件路径中添加 Environment.getExternalStorageDirectory().getPath() 或“/sdcard/”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多