0

I want to show the profile picture in the image view holder in my chat app. I have implemented firebase auth sign in methods like google and facebook. So I want to show the user image when they chat. I can show current user image by FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl(); But when I want to get another user's image? So I tried below method,

When the sign up to the app I can get their profile image and upload it to firebase storage. After that I can use it when in the chat head of the app. So below is the code

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 + "%");
            });
}

But is unreadable, I am getting this error:

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)

Please help me to fix it or suggest me another method to do my task

Lakpriya Senevirathna
  • 2,488
  • 2
  • 17
  • 35
  • 2
    You should edit your question to not make mention of "Firebase Storage", as you are essentially just storing the user's profile URL to your "Firebase Realtime Database". – samthecodingman Jul 10 '21 at 05:34

3 Answers3

1

You can uploading files to Firebase Storage from one of three things: 1) a local file, 2) an array of bytes, 3) a base64 encoded string of data. You are calling putFile with the URL of an image, which is not supported.

You will have to instead do a two-step approach:

  1. Download the data from the URL to the Android device,
  2. Then upload the data from the Android device to Storage.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

I would suggest you to do this before you uploading the profile picture to FirebaseStorage.

  1. Get the image url display on an ImageView.
  2. Convert the image to bytes.
    val bitmap = (binding.ivPicture.drawable as BitmapDrawable).bitmap
    val baos = ByteArrayOutputStream()
    bitmap.compress(
        Bitmap.CompressFormat.JPEG,
        30,
        baos
    )
    val data = baos.toByteArray()
    // Create file metadata including the content type
    val metadata = storageMetadata {
        contentType = "image/jpeg"
    }
  1. 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
                )
    
            }
        }
    }
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
  • But I need to get the firebase auth image, I have already know how to upload a image to firebase store! – Lakpriya Senevirathna Jul 10 '21 at 05:27
  • 2
    @LakpriyaSenevirathna You've misunderstood what this code does, you load the image from `user.getPhotoUrl()` to the `ImageView`, then in step 2 it takes the cached image in stored in the view and converts it to an array of bytes, which in step 3 is then uploaded to Cloud Storage – samthecodingman Jul 10 '21 at 05:31
  • yes. your method is also correct. visit my answer below! I think it is the best way to do! – Lakpriya Senevirathna Jul 10 '21 at 05:34
0

Finally I found a solution. I did something like this!

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

You will see image url in firebase database

If you use Google OAuth;

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

if you remove the =s96-c , you can get a quality image!

https://lh3.googleusercontent.com/a/AATXAJzFGstrkeyXMQUU9XIKEXIL8jgEgm1T-EKi3AW1

If you are using any other Oauth methods. it's working, I tried yahoo Oauth, It will retrieve the url of the profile image by currentFirebaseUser.getPhotoUrl().toString()

Lakpriya Senevirathna
  • 2,488
  • 2
  • 17
  • 35