Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
FireBase |谷歌登录|如何获取用户的谷歌图像并在android手机中下载_Android_Firebase_Firebase Authentication_Google Oauth_Google Signin - Fatal编程技术网

FireBase |谷歌登录|如何获取用户的谷歌图像并在android手机中下载

FireBase |谷歌登录|如何获取用户的谷歌图像并在android手机中下载,android,firebase,firebase-authentication,google-oauth,google-signin,Android,Firebase,Firebase Authentication,Google Oauth,Google Signin,我尝试使用以下代码获取用户的google个人资料图片,但这只提供缩略图大小的模糊照片: FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl(); 这是给我的uri,当转换为字符串时显示以下URL URL显示pic,但由于隐私问题,此处修改了几个数字: 我能够使用毕加索使用这个url在ImageView中显示图片,但不知道如何下载并存储在手机内存中 Picasso.get().load(FirebaseAuth.getInstance(

我尝试使用以下代码获取用户的google个人资料图片,但这只提供缩略图大小的模糊照片:

FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
这是给我的uri,当转换为字符串时显示以下URL URL显示pic,但由于隐私问题,此处修改了几个数字:

我能够使用毕加索使用这个url在ImageView中显示图片,但不知道如何下载并存储在手机内存中

Picasso.get().load(FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl().toString()).fit().into(profileImage);
我通过将getPhotoURL转换为位图尝试了以下操作:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(SplashActivity.this.getContentResolver(), userPhotoURLUri);
FileOutputStream fos = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
但这在第一行给了我一个例外:

FileNotFoundException:没有内容提供商:用于google getphotouri

试试这个:

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();
    }
权限:

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

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();
    }
权限:

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

您可以使用Android的下载管理器让它处理下载:

// 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);
此外,如果您希望应用程序执行操作以响应已完成的下载,则需要注册BroadcastReceiver filtering DownloadManager.ACTION\u download\u COMPLETE Intents并检查.enqueue返回的下载id

以下是有关DownloadManager和DownloadManager的详细信息。您可以使用请求自定义下载选项:


您可以使用Android的下载管理器处理下载:

// 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);
此外,如果您希望应用程序执行操作以响应已完成的下载,则需要注册BroadcastReceiver filtering DownloadManager.ACTION\u download\u COMPLETE Intents并检查.enqueue返回的下载id

以下是有关DownloadManager和DownloadManager的详细信息。您可以使用请求自定义下载选项:


以下代码对我有效

由于谷歌个人资料图片的url中不包含.jpg或.png,因此所有其他方法都不起作用

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();
         }

下面的代码对我有用

由于谷歌个人资料图片的url中不包含.jpg或.png,因此所有其他方法都不起作用

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();
         }

这不是我所问的,我已经在问题中提供了这个问题。我希望url保存为Android内存中的图像文件该图像正在保存,但其大小为0B&打开图像时,显示文件格式不受支持或文件已损坏。这不是我所问的,我已在问题中提供了此查询。我希望url保存为Android内存中的图像文件该图像正在保存,但其大小为0B&打开图像时,显示文件格式不受支持或文件已损坏。最佳解决方法是按照FatalCoder524的建议从imageview保存图像。为什么要存储它?这是一个非常小的,低质量的图像用于其他地方。如果您使用毕加索或Glide,它会自动为您缓存。我希望图像用作用户配置文件pic。我的想法是将其保存在本地,这样我就不必再次下载它&每次从云上再次下载activity@Mangesh他们有没有办法从谷歌获得原始质量的个人资料图片?我想你需要另一种方式。getCurrentUser.getPhotoUrl不会提供高质量。最好的解决方法是按照FatalCoder524的建议从imageview保存图像为什么要存储它?这是一个非常小的,低质量的图像用于其他地方。如果您使用毕加索或Glide,它会自动为您缓存。我希望图像用作用户配置文件pic。我的想法是将其保存在本地,这样我就不必再次下载它&每次从云上再次下载activity@Mangesh他们有没有办法从谷歌获得原始质量的个人资料图片?我想你需要另一种方式。getCurrentUser.getPhotoUrl不会提供高质量。