Android 将图像从库存储到其他文件夹
到目前为止,我所取得的成就是,我可以将从相机单击的图像存储到一个新文件夹中Android 将图像从库存储到其他文件夹,android,file,android-gallery,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)); st
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);
但我不知道如何将从gallery中选择的图像存储到我创建的同一文件夹中。请帮帮我。
提前感谢您。首先,从您从gallery获得的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以了解更多详细信息
上述解决方案对我有效,但有一些小改动: 相机单击的图像由动作图像捕获意图保存。而对于从gallery中为我们自己的应用程序拾取图像,我们需要保存来自ACTION_PICK intent的图像。在这种情况下,保存基本上是在ImageView中显示图像后将图像从gallery复制到您自己的应用程序文件夹 首先,您需要有目标文件:因此,请调用以下方法以获取应用程序的目录,该目录位于Android>数据>应用程序文件夹>文件>图片中:
private File mPhotoFile;
mPhotoFile=PhotoLab.get(getActivity()).getPhotoFile(mPhoto);
下面是getPhotoFile()函数
现在,您已经有了一个文件,可以在其中保存由image Intent拾取的图像。现在把动作称为“选择意图”
private static final int PICK_IMAGE_REQUEST=2;
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //Trying intent to pick image from gallery
pickIntent.setType("image/*");
Uri uriImagePath = Uri.fromFile(mPhotoFile);
pickIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriImagePath);
startActivityForResult(pickIntent, PICK_IMAGE_REQUEST);
在onActivityResult中收到结果后,以下是我的代码:
if(requestCode==PICK_IMAGE_REQUEST){
Picasso.with(getActivity()).load(data.getData()).fit().into(mPhotoView);
File file=getBitmapFile(data);
try {
copyFile(file, mPhotoFile);
}catch(IOException e){
Toast.makeText(getActivity(),"Cannot use Gallery, Try Camera!",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
获取数据后,我将其传递到下一个方法,以通过Intent.ACTION\u PICK查找拾取图像的绝对路径。以下是方法定义:
public File getBitmapFile(Intent data){
Uri selectedImage=data.getData();
Cursor cursor=getContext().getContentResolver().query(selectedImage, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
cursor.moveToFirst();
int idx=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
String selectedImagePath=cursor.getString(idx);
cursor.close();
return new File(selectedImagePath);
}
获取拾取图像的绝对路径后。我调用了copyFile()函数。具体如下。请记住,我已经生成了新的文件路径
public File getBitmapFile(Intent data){
Uri selectedImage=data.getData();
Cursor cursor=getContext().getContentResolver().query(selectedImage, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
cursor.moveToFirst();
int idx=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
String selectedImagePath=cursor.getString(idx);
cursor.close();
return new File(selectedImagePath);
}
我的目的地是mPhotoFile。我们首先生成的。
如果您想从gallery中选取图像并将其存储,以便在每次打开时在应用程序的imageView中进一步显示,则整个代码都会起作用。您正在创建项目名称的文件夹,只需将捕获图像复制到项目名称文件夹????希望您首先澄清..此主题可能会帮助您:Utils.getPath()返回我要创建新文件夹以存储图像的位置。其余部分由意图处理。但当您从多媒体资料中选择图像时,MediaStore.EXTRA_输出不起作用。那么,如何将从gallery中选择的图像存储到另一个文件夹中呢?getBitmapFile函数返回null,而答案缺少copyFile函数
public File getBitmapFile(Intent data){
Uri selectedImage=data.getData();
Cursor cursor=getContext().getContentResolver().query(selectedImage, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
cursor.moveToFirst();
int idx=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
String selectedImagePath=cursor.getString(idx);
cursor.close();
return new File(selectedImagePath);
}