FireBase | Google Sign-in | How to get user's google image & download it in android phone

Viewed 1102

I tried following code to get the user's google profile pic, but this is giving only thumbnail size blur photo:

FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();

This is giving me uri, which when converted to string shows following URL (URL is showing pic, but due to privacy modified few digits here):

https://lh3.googleusercontent.com/a-/AguE7mDKNdcXubEW0cMTTYzschAykXcWRQDYeMlHb8rf_g=s96-c

I am able to use this url to show picture in an ImageView using Picasso, but not sure how to download it & store in phone memory.

Picasso.get().load(FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl().toString()).fit().into(profileImage);

I tried following by converting getPhotoURL into bitmap:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(SplashActivity.this.getContentResolver(), userPhotoURLUri);
FileOutputStream fos = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();

But this is giving me exception at the very first line:

FileNotFoundException: No content provider: for google getphotouri

3 Answers

Following code worked for me.

As the google profile pic doesn't contains .jpg or .png in its url therefore all other methods are not working.

GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(YourActivity.this);

//Set the Image dimension here it will not reduce the image pixels 
googleProfilePic = acct.getPhotoUrl().toString().replace("s96-c", "s492-c");

Glide.with(MainActivity.this).load(googleProfilePic).asBitmap().into(new BitmapImageViewTarget(imageView) {
        @Override
        protected void setResource(Bitmap resource) {
                    FileOutputStream outStream = null;
                    File dir = new File(myfolderPath);
                    
                    String fileName = picName + ".jpg";
                    File outFile = new File(dir, fileName);

                    outStream = new FileOutputStream(outFile);
                    outStream.flush();
                    resource.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                    outStream.close();
         }

Try this:

Picasso.get().load(FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl()).fit().into(profileImage);
BitmapDrawable draw = (BitmapDrawable) profileImage.getDrawable();
Bitmap bitmap = draw.getBitmap();

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
try{
        FileOutputStream outStream = new FileOutputStream(outFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
        outStream.flush();
        outStream.close();
}catch (Exception e) {
        e.printStackTrace();
    }

Permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

You can use Android's Download Manager to have it handle the download:

// Create the Download Request
DownloadManager.Request downloadRequest = new DownloadManager.Request(myPhotoUri);

// Set the destination 
// (You can include the "SubPath/FileName" as the second argument if you want the file in a sub directory)
downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, myFileName);

// Display a notification while the download is in progress and after it's completed
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

// Allow the media scanner to find the file
downloadRequest.allowScanningByMediaScanner();

// Enqueue the download
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = downloadManager.enqueue(downloadRequest);

Additionally, if you want your app to perform an operation in response to the completed download, you would register a BroadcastReceiver filtering DownloadManager.ACTION_DOWNLOAD_COMPLETE Intents and check for the Download id returned by .enqueue().

Here's further information on DownloadManager and DownloadManager.Request you can use to customize your download options:

https://developer.android.com/reference/android/app/DownloadManager https://developer.android.com/reference/android/app/DownloadManager.Request

Related