1

I am trying to load an image into an ImageView from gallery using Picasso.

Here's the code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode!= Activity.RESULT_OK) return;

        switch (requestCode) {
            case PICK_GALLERY_IMAGE_REQUEST: {
                Uri imageUri = data.getData();
                Log.d(DEBGUG_TAG,"Image URI: "+imageUri.toString());


                Picasso picasso = Picasso.with(getApplicationContext());
                picasso.setLoggingEnabled(true);
                picasso.load(imageUri).error(R.drawable.c).into(testImgView, new com.squareup.picasso.Callback(){

                    @Override
                    public void onSuccess() {
                        Log.d(DEBGUG_TAG,"Success loading image from uri:PICASSO");
                    }

                    @Override
                    public void onError() {
                        Log.d(DEBGUG_TAG,"Cannot load image");
                    }
                });

The problem is while selecting an Image from Gallery it returns a file path

D/debug: Image URI: file:///storage/emulated/0/DCIM/Camera/IMG_20170126_211524.jpg

Which doesn't seems to work with Picasso as returns error and logs D/debug: Cannot load image in error method.

However choosing the same image from another app which returns Uri like : D/debug: Image URI: content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F90455/ORIGINAL/NONE/1757236944 is successful.

Any way to load image from file path?

1
  • In which sdk version your app is running ? Please check the logcat if you are getting FileUriExposedException Apr 10, 2017 at 7:40

3 Answers 3

5

Dont use file:// just use it like this file:

 Uri targetUri = data.getData();
            if (data.toString().contains("content:")) {
                imagePath = getRealPathFromURI(targetUri);
            } else if (data.toString().contains("file:")) {
                imagePath = targetUri.getPath();
            } else {
                imagePath = null;
            }

 public String getRealPathFromURI(Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = getContentResolver().query(contentUri, proj, null, null,
                null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
1
  • I was getting file using File file = new File(String.value(uri));, it worked when I changed it to File file = new File(uri.getPath());. Thanks.
    – user4035628
    May 31, 2017 at 6:36
2

Try following code:

File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}
1

Try ImageView's built-in method setImageURI for local files

testImgView.setImageURI(Uri.parse(imageUri));
1
  • Nope, Same issue, Guess it's not about picasso :P Apr 10, 2017 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.