The solution above works for me but with minor changes:
The Image clicked by the camera is saved by the ACTION_IMAGE_CAPTURE intent. Whereas for picking an image from gallery for our own app, we need to save the Image coming from ACTION_PICK intent. Saving in this case is basically copying image from gallery to your own app folder after showing it in your ImageView.
First you need to have the destination file: So call the following method to get the directory of your app which is in Android>data>YOUR APP FOLDER>Files>Pictures:
private File mPhotoFile;
mPhotoFile=PhotoLab.get(getActivity()).getPhotoFile(mPhoto);
Here is the getPhotoFile() Function
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());
}
Now you have the file where you want to save the image picked by Image Intent. Now call the ACTION_PICK 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);
After receiving the result in onActivityResult Following is my code:
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();
}
}
After Getting the data, I passed it onto the next method to find the absolute path of the picked image by Intent.ACTION_PICK. Following is the method definition:
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);
}
After getting the absolute path of the picked image. I called the copyFile() function. Which is as follows. Remember that I already generated my new file path.
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);
}
The Destination for me was mPhotoFile. That we generated first.
This whole code will work if you want to pick image from gallery and store it to further show in the imageView of your app every time you open it.