[英]Copying file from asset into sd card in android
我正在使用此代碼將資產文件夾中的文件處理到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)
嘗試先創建父目錄,也許它們不存在:
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";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.