上述解決方案對我的作品,但有細微的變化:
相機單擊圖像由ACTION_IMAGE_CAPTURE意圖保存。而從我們自己的應用程序中從圖庫中選取圖像,我們需要保存來自ACTION_PICKT意圖的圖像。在這種情況下保存基本上是將圖像從圖庫複製到您的ImageView中後顯示在您自己的應用程序文件夾中。
首先你需要有目標文件:所以調用下面的方法來讓你的應用程序的目錄是在安卓>資料>您的應用程序文件夾>文件>圖片:
private File mPhotoFile;
mPhotoFile=PhotoLab.get(getActivity()).getPhotoFile(mPhoto);
這裏是getPhotoFile()函數
public File getPhotoFile(Photo photo){
File externalFilesDir=mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);//getExternalStorageDirectory().toString());
if(externalFilesDir==null){
return null;
}
return new File(externalFilesDir, photo.getPhotoFilename());
}
現在,您已經有了您想要保存Image Intent所選圖像的文件。現在調用ACTION_PICKT意圖。
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_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。我們首先生成的。 如果您想從圖庫中挑選圖像並將其存儲,以便在您每次打開應用程序時在應用程序的imageView中進一步顯示,則此整個代碼將起作用。
你正在創建你的項目名稱的文件夾,你只需將捕獲的圖像複製到你的項目名稱文件夾?想要你先澄清.. –
本主題可能會幫助您:http://stackoverflow.com/questions/11846108/android-saving-bitmap-to-sd-card –
Utils.getPath()返回我想要創建一個新文件夾來存儲的位置圖片。休息是由意圖處理的。但是當您從圖庫中選擇圖像時,MediaStore.EXTRA_OUTPUT不起作用。那麼如何將從圖庫中選擇的圖像存儲到另一個文件夾? – AmeyaG