android 拷贝assets目录文件到sdcard
时间: 2023-12-04 09:03:17 浏览: 109
在 Android 中,可以使用以下代码将 assets 目录中的文件拷贝到 SD 卡中:
```java
private void copyAssetsToSDCard() {
String[] files = null;
try {
files = getAssets().list("");
} catch (IOException e) {
e.printStackTrace();
}
for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = getAssets().open(filename);
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + filename;
out = new FileOutputStream(filePath);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
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);
}
}
```
需要注意的是,同样需要在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
阅读全文