从复制图像入资产到SD卡上安装Android应用程序 [英] copy images from assest to sd card on install android application
本文介绍了从复制图像入资产到SD卡上安装Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些照片应显示在应用程序中,问:如何我在资产文件夹中的code把影像复制到文件夹的用户安装应用程序的SC卡下在手机上
i have some images should be displayed in the Application, the Q. is how to copy the images i am putting in the assets folder in the code to a folder under the SC card of the user on install the application on the phone
推荐答案
这个尝试,
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream("/sdcard/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}
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);
}
}
不要忘了在你的manifiest添加此权限
Don't forget to add this permission in your manifiest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这篇关于从复制图像入资产到SD卡上安装Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文