1
votes

I am using Google+ Sign In to get an instance of GoogleSignInAccount and I can correctly load the users name and email without issues, so I know it is correctly set up. Then I get the Uri of the users profile picture and am trying to set that as the icon for an ImageButton.

The variable userPhoto is an android.net.Uri grabbed from GoogleSignInAccount. The photo, just to be clear, is not present on the device.

This is the code I'm using right now to no avail:

profileButton = (ImageButton) navHeaderView.findViewById(R.id.navHeaderMainImageButton);

    try {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), userPhoto);
        profileButton.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }

This is the error I get:

    W/System.err: java.io.FileNotFoundException: No content provider: https://lh5.googleusercontent.com/-s7XGuonbDRk/AAAAAAAAAAI/AAAAAAAAFv8/htGjmtY1xD8/photo.jpg
    W/System.err:     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1117)
    W/System.err:     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:970)
    W/System.err:     at android.content.ContentResolver.openInputStream(ContentResolver.java:695)
    W/System.err:     at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:859)
    W/System.err:     at com.seranth.sertutor.MainActivity.onCreate(MainActivity.java:136)
    W/System.err:     at android.app.Activity.performCreate(Activity.java:6500)
    W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
    W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
    W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
    W/System.err:     at android.app.ActivityThread.access$1000(ActivityThread.java:198)
    W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
    W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
    W/System.err:     at android.os.Looper.loop(Looper.java:145)
    W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6837)
    W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
    W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
    W/Resources: Converting to string: TypedValue{t=0x5/d=0x3801 a=1 r=0x10500d1}
    W/OpenGLRenderer: Program(5ea33b00) : invalid binary - source difference(vs(201:201)/fs(94:36705)-b0000000800000000)

If I follow the link thrown as an error for file not found I find the photo (that's me btw): https://lh5.googleusercontent.com/-s7XGuonbDRk/AAAAAAAAAAI/AAAAAAAAFv8/htGjmtY1xD8/photo.jpg

So the question now is really, why doesn't the code above work and is there another way of getting a bitmap from an Uri?

I can of course get the URL of the photo from the Uri and then download it to the device, but I'm hoping to not have to do that. If that is the only solution you know of then please supply working code samples of how to download a URL file AND how to convert the downloaded URL into a bitmap.

2

2 Answers

1
votes

MediaStore.Images.Media.getBitmap(this.getContentResolver(), userPhoto);

This loads a URI from the filesystem. It does not load a URL from the network.

You need to use a networking library to get the byte stream (in a thread) and convert that to a bitmap.

You can use third party networking libraries and there are also third party Network enabled ImageViews to do this. In any case, you have to add the code yourself.

0
votes

I suggest another way, using ImageView instead, then you can call setOnClickListener on that ImageView.

private void handleSignInResult(GoogleSignInResult result) {    
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        ...
        Uri uri = acct.getPhotoUrl();
        Picasso.with(mContext)
                .load(uri)
                .placeholder(android.R.drawable.sym_def_app_icon)
                .error(android.R.drawable.sym_def_app_icon)
                .into(mProfileImageView);
        // then call mProfileImageView.setOnClickListener();...

        updateUI(true);
    } else {
        // Signed out, show unauthenticated UI.
        updateUI(false);
    }
}