添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Firebase Auth: 我想获得当前的用户资料照片,并将其上传到Firebase数据库或存储空间。

0 人关注

我想在我的聊天应用程序中的图像视图夹中显示个人资料图片。我已经实现了Firebase auth sign in方法,如谷歌和Facebook。所以我想在用户聊天时显示他们的图片。我可以通过 FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl(); 显示当前用户的图片。但是,当我想获得另一个用户的图像时?所以我尝试了以下方法。

当他们注册到应用程序时,我可以获得他们的个人资料图片,并将其上传到Firebase存储。之后,我可以在应用程序的聊天头中使用它。所以下面的代码是

Uri dpUri = currentFirebaseUser.getPhotoUrl();
if (dpUri != null) {
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Uploading...");
    progressDialog.show();
    final StorageReference ref = storageReference.child(currentFirebaseUser.getUid());
    ref.putFile(dpUri)
            .addOnSuccessListener(taskSnapshot -> {
                ref.getDownloadUrl().addOnSuccessListener(uri -> databaseReference.child("Users").child(currentFirebaseUser.getUid()).child("URL").setValue(uri.toString()));
                progressDialog.dismiss();
                LocationOn();
            .addOnFailureListener(e -> {
                progressDialog.dismiss();
                Toast.makeText(SignActivity.this, "Failed " + e.getMessage(), Toast.LENGTH_SHORT).show();
            .addOnProgressListener(taskSnapshot -> {
                double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
                        .getTotalByteCount());
                progressDialog.setMessage("Uploaded " + (int) progress + "%");

但是无法阅读,我得到了这个错误。

E/UploadTask: could not locate file for uploading:https://lh3.googleusercontent.com/a/AATXAJzFGstrkeyXMQUU9XIKEXIL8jgEgm1T-EKi3AW1=s96-c
E/StorageException: StorageException has occurred.
    An unknown error occurred, please check the HTTP result code and inner exception for server response.
     Code: -13000 HttpResult: 0
E/StorageException: No content provider: https://lh3.googleusercontent.com/a/AATXAJzFGstrkeyXMQUU9XIKEXIL8jgEgm1T-EKi3AW1=s96-c
    java.io.FileNotFoundException: No content provider: https://lh3.googleusercontent.com/a/AATXAJzFGstrkeyXMQUU9XIKEXIL8jgEgm1T-EKi3AW1=s96-c
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1979)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1808)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:1485)
        at com.google.firebase.storage.UploadTask.<init>(UploadTask.java:131)
        at com.google.firebase.storage.StorageReference.putFile(StorageReference.java:240)
        at com.myapp.SignActivity.uploadImage(SignActivity.java:143)
        at com.axresapps.wheresapplk.SignActivity.lambda$onCreate$2$SignActivity(SignActivity.java:123)
        at com.myapp.-$$Lambda$SignActivity$__O5f7J8hhcqZRFrdV0j4-3CaQk.onClick(Unknown Source:4)
        at android.view.View.performClick(View.java:8160)
        at android.widget.TextView.performClick(TextView.java:16222)
        at android.view.View.performClickInternal(View.java:8137)
        at android.view.View.access$3700(View.java:888)
        at android.view.View$PerformClick.run(View.java:30236)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8512)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
E/StorageException: StorageException has occurred.
    An unknown error occurred, please check the HTTP result code and inner exception for server response.
     Code: -13000 HttpResult: 0
E/StorageException: No content provider: https://lh3.googleusercontent.com/a/AATXAJzFGstrkeyXMQUU9XIKEXIL8jgEgm1T-EKi3AW1=s96-c
    java.io.FileNotFoundException: No content provider: https://lh3.googleusercontent.com/a/AATXAJzFGstrkeyXMQUU9XIKEXIL8jgEgm1T-EKi3AW1=s96-c
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1979)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1808)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:1485)
        at com.google.firebase.storage.UploadTask.<init>(UploadTask.java:131)
        at com.google.firebase.storage.StorageReference.putFile(StorageReference.java:240)
        at com.axresapps.wheresapplk.SignActivity.uploadImage(SignActivity.java:143)
        at com.myapp.SignActivity.lambda$onCreate$2$SignActivity(SignActivity.java:123)
        at com.axresapps.wheresapplk.-$$Lambda$SignActivity$__O5f7J8hhcqZRFrdV0j4-3CaQk.onClick(Unknown Source:4)
        at android.view.View.performClick(View.java:8160)
        at android.widget.TextView.performClick(TextView.java:16222)
        at android.view.View.performClickInternal(View.java:8137)
        at android.view.View.access$3700(View.java:888)
        at android.view.View$PerformClick.run(View.java:30236)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8512)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
E/StorageException: StorageException has occurred.
    An unknown error occurred, please check the HTTP result code and inner exception for server response.
     Code: -13000 HttpResult: 0
E/StorageException: No content provider: https://lh3.googleusercontent.com/a/AATXAJzFGstrkeyXMQUU9XIKEXIL8jgEgm1T-EKi3AW1=s96-c
    java.io.FileNotFoundException: No content provider: https://lh3.googleusercontent.com/a/AATXAJzFGstrkeyXMQUU9XIKEXIL8jgEgm1T-EKi3AW1=s96-c
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1979)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1808)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:1485)
        at com.google.firebase.storage.UploadTask.<init>(UploadTask.java:131)
        at com.google.firebase.storage.StorageReference.putFile(StorageReference.java:240)
        at com.axresapps.wheresapplk.SignActivity.uploadImage(SignActivity.java:143)
        at com.axresapps.wheresapplk.SignActivity.lambda$onCreate$2$SignActivity(SignActivity.java:123)
        at com.axresapps.wheresapplk.-$$Lambda$SignActivity$__O5f7J8hhcqZRFrdV0j4-3CaQk.onClick(Unknown Source:4)
        at android.view.View.performClick(View.java:8160)
        at android.widget.TextView.performClick(TextView.java:16222)
        at android.view.View.performClickInternal(View.java:8137)
        at android.view.View.access$3700(View.java:888)
        at android.view.View$PerformClick.run(View.java:30236)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8512)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

请帮助我解决这个问题或建议我用其他方法来完成我的任务。

1 个评论
你应该编辑你的问题,不要提到 "Firebase存储",因为你本质上只是将用户的资料URL存储到你的 "Firebase实时数据库 "中。
android
firebase
firebase-realtime-database
firebase-storage
Lakpriya Senevirathna
Lakpriya Senevirathna
发布于 2021-07-10
3 个回答
Frank van Puffelen
Frank van Puffelen
发布于 2021-07-10
0 人赞同

你可以从以下三种情况中的一种上传文件到Firebase存储。1)一个本地文件,2)一个字节数组,3)一个base64编码的数据字符串。你在调用 putFile 时使用了一个图片的URL,这不被支持。

你将不得不采取两步走的办法。

  • Download the data from the URL to the Android device,
  • Then upload the data from the Android device to Storage.
  • Upload image from URL to Firebase Storage
  • Ticherhaz FreePalestine
    Ticherhaz FreePalestine
    发布于 2021-07-10
    0 人赞同

    我建议你在上传个人资料图片到FirebaseStorage之前先这样做。

  • Get the image url display on an ImageView .
  • Convert the image to bytes.
  •     val bitmap = (binding.ivPicture.drawable as BitmapDrawable).bitmap
        val baos = ByteArrayOutputStream()
        bitmap.compress(
            Bitmap.CompressFormat.JPEG,
        val data = baos.toByteArray()
        // Create file metadata including the content type
        val metadata = storageMetadata {
            contentType = "image/jpeg"
    
  • Store the bytes to FirebaseStorage
  •     val uploadTask = storage.putBytes(data, metadata)
        uploadTask.addOnFailureListener {
            // Handle unsuccessful uploads
            Tools.showToast(
                this,
                "Error uploading image: " + it.message
        }.addOnSuccessListener {
            storage.downloadUrl.addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    //Get download url to store in the database.
                    val profileUrl = task.result.toString()
                } else {
                    Tools.showToast(
                        this,
                        "Error saving: " + task.exception?.message
        
    但是我需要得到Firebase的授权图片,我已经知道如何将图片上传到Firebase商店了
    @LakpriyaSenevirathna 你误解了这段代码的作用,你把图片从 user.getPhotoUrl() 加载到 ImageView ,然后在第二步,它把存储在视图中的缓存图片转换为一个字节数组,然后在第三步,它被上传到云存储。
    是的。你的方法也是正确的。请看我下面的答案!我想这是最好的方法!
    Lakpriya Senevirathna
    Lakpriya Senevirathna
    发布于 2021-07-10
    0 人赞同

    最后我找到了一个解决方案。我做了类似这样的事情!

     if(currentFirebaseUser.getPhotoUrl()!=null){
         databaseReference.child("Users").child(currentFirebaseUser.getUid()).child("URL").setValue(currentFirebaseUser.getPhotoUrl().toString());
    

    你会在Firebase数据库中看到图片的网址

    如果你使用谷歌OAuth。

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