I'm trying to write an image picker which will pick an image from the gallery and set it as the source of an ImageView. I'm using the code given below to do this but after I select an image in the gallery and the app returns to the Fragment I get the error :
I/PICTURE: Path: /storage/emulated/0/DCIM/Camera/IMG_20160409_130537213.jpg
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20160409_130537213.jpg: open failed: EACCES (Permission denied)
I/System.out: resolveUri failed on bad bitmap uri: /storage/emulated/0/DCIM/Camera/IMG_20160409_130537213.jpg
Here is the code:
Fragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_account, container, false);
........
........
profile_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PICTURE);
//SELECT_PICTURE = 1; defined in the fragment class
}
});
//The onActivityResult method
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_PICTURE
&& resultCode == Activity.RESULT_OK) {
String path = getPathFromCameraData(data, this.getActivity());
Log.i("PICTURE", "Path: " + path);
if (path != null) {
profile_img.setImageURI(Uri.parse(path));
}
}
}
//The string used to get the file(image) location
public static String getPathFromCameraData(Intent data, Context context) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return picturePath;
}
In the manifest, I've correctly stated that the app needs access to the device storage
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
....
</application>
</manifest>
What could be the problem? Any help is appreciated. Thank you!