Reputation: 5109
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
Upvotes: 0
Views: 1178
Reputation: 5109
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()
Upvotes: 0
Reputation: 2377
I would suggest you to do this before you uploading the profile picture to FirebaseStorage.
ImageView
. 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"
}
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
)
}
}
}
Upvotes: 1
Reputation: 598728
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:
Also see:
Upvotes: 1