square / picasso

A powerful image downloading and caching library for Android
https://square.github.io/picasso/
Apache License 2.0
18.71k stars 3.97k forks source link

Picasso crashes when loading image with path/Uri when loading image from gallery, but with Android bitmap it works. #931

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hello!

First of all, i wanted to say that I have a lot of respect for your work, and your API is very easy to use. Congrats for that!

I am trying to load an image with path/uri into an imageView using Picasso, but it crashes. I have the following code:

public static final int GET_FROM_GALLERY = 3;

@OnClick(R.id.imageView_signUpConfirmedEditProfileLayout_profilePicture)
    public void setProfilePicture() {

        startActivityForResult(new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //Detects request codes
        if(requestCode == GET_FROM_GALLERY && resultCode == Activity.RESULT_OK && data != null) {
            selectedImageUri = data.getData();
            Bitmap bitmap = null;
            Bitmap scaledBitmap;
            String selectedImagePath;
            selectedImagePath = getPath(selectedImageUri);

            Picasso.with(context)
                    .load(selectedImagePath)
                    .resize(800, 800)
                    .centerCrop()
                    .into(profilePictureImageView);
    }
}

 public String getPath(Uri uri) {
        // just some safety built in
        if( uri == null ) {
            // TODO perform some logging or show user feedback
            return null;
        }
        // try to retrieve the image from the media store first
        // this will only work for images selected from gallery
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if( cursor != null ){
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        // this is our fallback here
        return uri.getPath();
    }

Using it like this, it crashes. Here is the stack trace:

03-17 12:44:26.715  27961-27961/com.entu.artapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { dat=content://media/external/images/media/4719 }} to activity {com.entu.artapp/com.entu.artapp.activity.SignUpConfirmedEditProfileActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3103)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3146)
            at android.app.ActivityThread.access$1100(ActivityThread.java:132)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1187)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4575)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:432)
            at com.pkmmte.view.CircularImageView.refreshBitmapShader(CircularImageView.java:341)
            at com.pkmmte.view.CircularImageView.invalidate(CircularImageView.java:262)
            at android.widget.ImageView.setImageDrawable(ImageView.java:364)
            at com.squareup.picasso.PicassoDrawable.setPlaceholder(PicassoDrawable.java:61)
            at com.squareup.picasso.RequestCreator.into(RequestCreator.java:653)
            at com.squareup.picasso.RequestCreator.into(RequestCreator.java:590)
            at com.entu.artapp.activity.SignUpConfirmedEditProfileActivity.onActivityResult(SignUpConfirmedEditProfileActivity.java:141)
            at android.app.Activity.dispatchActivityResult(Activity.java:4649)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3099)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3146)
            at android.app.ActivityThread.access$1100(ActivityThread.java:132)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1187)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4575)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

I went with the debugger from start to end, the path is shown correctly, but the app crashed. Line 141 is ".into(profilePictureImageView)".

Oddly, but when I load the image with default Android methods, it works. Here is the code for that:

bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
profilePictureImageView.setImageBitmap(source);
profilePictureHolderImageView.setVisibility(View.GONE);

I don't understand why. I read dozens of stackoverflow articles for this, but i did not get to any resolve.

Can you kindly help me with this problem?

Thanks, cheers!

ghost commented 9 years ago

I apologize for the bad code formatting, i did not know how.

dnkoutso commented 9 years ago

This does not seem to be a picasso problem. Picasso finishes the requests and sets the drawable to your custom CircularImageView.

Try using noPlaceholder() in your request..

ghost commented 9 years ago

It worked! Thanks you! Adding .noPlaceHolder() solved the problem. Continue the great work!

rishabhsri20 commented 9 years ago

@dnkoutso - amazing solution.

ret702 commented 8 years ago

I just found this solution, did you know data.getData() has a get path method built in? data.getData().getPath().

Zapnologica commented 8 years ago

Thank you so much for this, I was having the same issue with "CircularImageView". and noPlaceholder() seemed to do the job