我想要把assets中的所有文件都复制到/sdcard/中,并在一个线程中完成这个操作,具体该如何做呢?
原问题:Android: How to copy files in 'assets' to sdcard?
欢迎选择我的课程,让我们一起见证您的进步~~
答:rciovati (最佳答案) 通常來說,我會採用以下方法:
private void copyAssets() { AssetManager assetManager = getAssets(); String[] files = null; try { files = assetManager.list(""); } catch (IOException e) { Log.e("tag", "Failed to get asset file list.", e); } for(String filename : files) { InputStream in = null; OutputStream out = null; try { in = assetManager.open(filename); File outFile = new File(getExternalFilesDir(null), filename); out = new FileOutputStream(outFile); copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch(IOException e) { Log.e("tag", "Failed to copy asset file: " + filename, e); } } } 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); } }
答:DannyA 基於上述方法,我做了一些調整,以便於將資料可以匯入子資料夾: ...
copyFileOrDir("myrootdir");
...
private void copyFileOrDir(String path) { AssetManager assetManager = this.getAssets(); String assets[] = null; try { assets = assetManager.list(path); if (assets.length == 0) { copyFile(path); } else { String fullPath = "/data/data/" + this.getPackageName() + "/" + path; File dir = new File(fullPath); if (!dir.exists()) dir.mkdir(); for (int i = 0; i < assets.length; ++i) { copyFileOrDir(path + "/" + assets[i]); } } } catch (IOException ex) { Log.e("tag", "I/O Exception", ex); } } private void copyFile(String filename) { AssetManager assetManager = this.getAssets(); InputStream in = null; OutputStream out = null; try { in = assetManager.open(filename); String newFileName = "/data/data/" + this.getPackageName() + "/" + filename; out = new FileOutputStream(newFileName); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); in = null; out.flush(); out.close(); out = null; } catch (Exception e) { Log.e("tag", e.getMessage()); } }
答:JPM 我知道這個問題有很多人解答過,但是我有一個相對來說更便捷的方法可以直接將asset中的文件複製到sdcard,它不需要進行“for”循環語句,但還是要用到File Streams和Channels。
AssetManager am = context.getAssets(); AssetFileDescriptor afd = null; try { afd = am.openFd( "MyFile.dat"); // Create new file to copy into. File file = new File(Environment.getExternalStorageDirectory() + java.io.File.separator + "NewFile.dat"); file.createNewFile(); copyFdToFile(afd.getFileDescriptor(), file); } catch (IOException e) { e.printStackTrace(); }
不需要循環的複製檔案方法:
public static void copyFdToFile(FileDescriptor src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
答:gnac 我的建議是採用「for」迴圈語句,方法如下:
for(String filename : files) { InputStream in = null; OutputStream out = null; try { in = assetManager.open(filename); out = new FileOutputStream("/sdcard/" + filename); ... }
答:rciovati
(最佳答案)
通常來說,我會採用以下方法:
答:DannyA
基於上述方法,我做了一些調整,以便於將資料可以匯入子資料夾:
...
...
答:JPM
我知道這個問題有很多人解答過,但是我有一個相對來說更便捷的方法可以直接將asset中的文件複製到sdcard,它不需要進行“for”循環語句,但還是要用到File Streams和Channels。
不需要循環的複製檔案方法:
答:gnac
我的建議是採用「for」迴圈語句,方法如下: