0

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!

Chinmay Dabke
  • 5,070
  • 11
  • 40
  • 63
  • 1
    https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it Beyond that, your code will not work for images on removable storage, and you are doing disk I/O on the main application thread. Please use [an image-loading library](http://android-arsenal.com/tag/46), passing it the `Uri` that you get in `onActivityResult()`. Your chosen library (e.g., Picasso) should handle [properly consuming the content from that `Uri`](https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html). – CommonsWare Apr 12 '16 at 17:27

2 Answers2

3

Do away with getPathFromCameraData() and change

profile_img.setImageURI(Uri.parse(path));

to

profile_img.setImageURI(data.getData());
greenapps
  • 11,154
  • 2
  • 16
  • 19
2

If you are picking image from gallery - you need this permission

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
alex
  • 23
  • 6