Ame*_*yaG 3 android file android-gallery
到目前为止,我所取得的成就是我可以将从相机点击的图像存储到一个新文件夹
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);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将从图库中选择的图像存储到我创建的同一个文件夹中。请帮我。先感谢您。
小智 6
首先,从您从画廊获得的 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);
}
Run Code Online (Sandbox Code Playgroud)
现在将图像复制到另一个位置,
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);
Run Code Online (Sandbox Code Playgroud)
查看网址了解更多详情,
| 归档时间: |
|
| 查看次数: |
9297 次 |
| 最近记录: |