Android如何将文件夹从内部存储器复制到外部
问题描述:
我需要找到一种方法来创建从我的设备的内部存储中的特定文件夹到外部存储中特定文件夹的文件。Android如何将文件夹从内部存储器复制到外部
实施例:
- 我在内部存储在
data/data/app_package/files/documents/server/userId/storage/
50的图像文件。 - 我想在该目录中的所有文件复制到
/sdcard/Documents/Server/UserId/Storage/
这个想法是,在某些情况下,也许我将不得不像移动和50MB的文件也许更多。任何建议我怎么能做到这一点?
答
试试这个代码
private void copyToFolder(String path) throws IOException {
File selectedImage = new File(path);
if (selectedImage.exists()) {
String wall = selectedImage.getName();
in = getContentResolver().openInputStream(selectedImageUri);
out = new FileOutputStream("/sdcard/wallpapers/" + wall);
copyFile(in , out); in .close(); in = null;
out.flush();
out.close();
out = null;
} else {
System.out.println("Does not exist");
}
}
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);
}
}