到目前为止我已经实现的是我可以将从相机点击的图像存储到一个新文件夹
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
f = new File(Utils.getPath(), new Date().getTime() + ".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, TAKE_PHOTO);
但我不知道如何将从图库中选择的图像存储到我创建的同一文件夹中。请帮我。 提前谢谢你。
最佳答案
首先,从您从画廊获得的 URI 获取真实路径。
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
startManagingCursor(cursor);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
现在将图像复制到另一个位置,
private void copyFile(File sourceFile, File destFile) throws IOException {
if (!sourceFile.exists()) {
return;
}
FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
File destFile = new File("Dest path");
copyFile(new File(getPath(data.getData())), destFile);
查看 url 以获取更多详细信息,
关于android - 将图库中的图像存储到不同的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36467907/