Profile image not showing after saving(it just sho

2020-02-07 06:39发布

问题:

I am trying to upload user profile image and store in firebase.My image successfully stores in firebase storage and in the user database.

When I upload the image it shows on my screen,but once I save and reopen the settings to see the profile image,it does not show,it just shows blank white screen(but the name and phone show when reopened,only image does not show)

I have attached the code that gets value from db and shows in ui and also added the saveUserInformation() which is called when the user clicks the save image button and this function saves name,phone and profileimage in db. I have added screenshot of database below and also added the snippet of error I am getting.

What am I doing wrong?

This is what glide outputs when i console logged it : profile img here:Target for: android.support.v7.widget.AppCompatImageView{528bbb2 V.ED..C.. ......ID 0,0-300,300 #7f0a00b9 app:id/profileImage}[CDS]rx timeout:0

 mCustomerDatabase.addValueEventListener(object :ValueEventListener{
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                if(dataSnapshot.exists() && dataSnapshot.childrenCount>0){
                    val map:Map<*,*> = dataSnapshot.value as Map<*,*>
                    if(map["name"] !=null){
                        mName=map["name"].toString()
                        mNameField.setText(mName)
                    }
                    if(map["phone"]!=null){
                        mPhone=map["phone"].toString()
                        mPhoneField.setText(mPhone)
                    }
                    if(map["profileImageUrl"]!=null){
                        mProfileImageUrl=map["profileImageUrl"].toString()

                        Glide.with(applicationContext).load(mProfileImageUrl).into(mProfileImage)
                        val a=Glide.with(applicationContext).load(mProfileImageUrl).into(mProfileImage)

                        print("profile img here:$a")

                    }

                }

            }

            override fun onCancelled(p0: DatabaseError) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
        })

    private fun saveUserInformation(){
        mName=mNameField.text.toString()
        mPhone=mPhoneField.text.toString()
        val userInfo=HashMap<String,Any>()
        userInfo.put("name",mName)
        userInfo.put("phone",mPhone)
        mCustomerDatabase.updateChildren(userInfo)
        if(resultUri!=null){
            val imagePath:StorageReference=mStorage.child("customer_profile_image").child(userId).child(resultUri.lastPathSegment )
            val uploadTask:UploadTask=imagePath.putFile(resultUri)
            uploadTask.addOnSuccessListener(OnSuccessListener <UploadTask.TaskSnapshot>(){taskSnapshot->
                    val downloadUrl: Task<Uri> =taskSnapshot.storage.downloadUrl
                    val newImage= HashMap<String,Any>()
                    newImage["profileImageUrl"] = downloadUrl.toString()
                    mCustomerDatabase.updateChildren(newImage)

            }).addOnFailureListener(OnFailureListener() {
                Toast.makeText(this,"Profile image not uploaded",Toast.LENGTH_SHORT).show()
            })
        }
        finish()
    }

Load failed for com.google.android.gms.tasks.zzu@c0645c1 with size [300x300]
    class com.bumptech.glide.load.engine.GlideException: Failed to load resource
    There were 3 causes:
    java.io.FileNotFoundException(/com.google.android.gms.tasks.zzu@c0645c1: open failed: ENOENT (No such file or directory))
    java.io.FileNotFoundException(No such file or directory)
    java.io.FileNotFoundException(No such file or directory)
     call GlideException#logRootCauses(String) for more detail
      Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, LOCAL
    There was 1 cause:
    java.io.FileNotFoundException(/com.google.android.gms.tasks.zzu@c0645c1: open failed: ENOENT (No such file or directory))
     call GlideException#logRootCauses(String) for more detail
        Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetch failed
    There was 1 cause:
    java.io.FileNotFoundException(/com.google.android.gms.tasks.zzu@c0645c1: open failed: ENOENT (No such file or directory))
     call GlideException#logRootCauses(String) for more detail
          Cause (1 of 1): class java.io.FileNotFoundException: /com.google.android.gms.tasks.zzu@c0645c1: open failed: ENOENT (No such file or directory)
      Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class android.os.ParcelFileDescriptor, LOCAL
    There was 1 cause:
    java.io.FileNotFoundException(No such file or directory)
     call GlideException#logRootCauses(String) for more detail
        Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetch failed
    There was 1 cause:
    java.io.FileNotFoundException(No such file or directory)
     call GlideException#logRootCauses(String) for more detail
          Cause (1 of 1): class java.io.FileNotFoundException: No such file or directory
      Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class android.content.res.AssetFileDescriptor, LOCAL
    There was 1 cause:
    java.io.FileNotFoundException(No such file or directory)
     call GlideException#logRootCauses(String) for more detail
        Cause (1 of 1): class java.io.FileNotFoundException: No such file or directory
02-04 15:21:14.109 25281-25281/com.sarah.taxi I/Glide: Root cause (1 of 3)
    java.io.FileNotFoundException: /com.google.android.gms.tasks.zzu@c0645c1: open failed: ENOENT (No such file or directory)
        at libcore.io.IoBridge.open(IoBridge.java:487)
        at java.io.FileInputStream.<init>(FileInputStream.java:76)
        at java.io.FileInputStream.<init>(FileInputStream.java:103)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:689)
        at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResourceFromUri(StreamLocalUriFetcher.java:85)
        at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResource(StreamLocalUriFetcher.java:60)
        at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResource(StreamLocalUriFetcher.java:15)
        at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:99)
        at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
        at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
        at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272)
        at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
        at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446)
     Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
        at libcore.io.Posix.open(Native Method)
        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
        at libcore.io.IoBridge.open(IoBridge.java:473)
        at java.io.FileInputStream.<init>(FileInputStream.java:76) 
        at java.io.FileInputStream.<init>(FileInputStream.java:103) 
        at android.content.ContentResolver.openInputStream(ContentResolver.java:689) 
        at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResourceFromUri(StreamLocalUriFetcher.java:85) 
        at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResource(StreamLocalUriFetcher.java:60) 
        at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResource(StreamLocalUriFetcher.java:15) 
        at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44) 
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:99) 
        at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62) 
        at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302) 
        at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272) 
        at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
        at java.lang.Thread.run(Thread.java:818) 
        at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446) 
02-04 15:21:14.110 25281-25281/com.sarah.taxi I/Glide: Root cause (2 of 3)
    java.io.FileNotFoundException: No such file or directory
        at android.os.Parcel.openFileDescriptor(Native Method)
        at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:270)
        at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:200)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:966)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:894)
        at com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher.loadResource(FileDescriptorLocalUriFetcher.java:22)
        at com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher.loadResource(FileDescriptorLocalUriFetcher.java:14)
        at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:99)
        at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
        at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
        at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherFailed(DecodeJob.java:397)
        at com.bumptech.glide.load.engine.SourceGenerator.onLoadFailed(SourceGenerator.java:119)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.startNextOrFail(MultiModelLoader.java:153)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onLoadFailed(MultiModelLoader.java:144)
        at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:49)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:99)
        at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
        at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
        at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272)
        at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
        at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446)
    Root cause (3 of 3)
    java.io.FileNotFoundException: No such file or directory
        at android.os.Parcel.openFileDescriptor(Native Method)
        at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:270)
        at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:200)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:966)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:894)
        at com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:22)
        at com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:13)
        at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)
        at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
        at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
        at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherFailed(DecodeJob.java:397)
        at com.bumptech.glide.load.engine.SourceGenerator.onLoadFailed(SourceGenerator.java:119)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.startNextOrFail(MultiModelLoader.java:153)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onLoadFailed(MultiModelLoader.java:144)
        at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:49)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:99)
        at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
        at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
        at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherFailed(DecodeJob.java:397)
        at com.bumptech.glide.load.engine.SourceGenerator.onLoadFailed(SourceGenerator.java:119)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.startNextOrFail(MultiModelLoader.java:153)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onLoadFailed(MultiModelLoader.java:144)
        at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:49)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:99)
        at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
        at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
        at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272)
        at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
        at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446)

enter image description here

回答1:

You are getting the following error:

Load failed for com.google.android.gms.tasks.zzu@c0645c1

Because you are trying to display an image that does not exist. As I see in your database, the profileImageUrl property holds an image url that is not valid.

See the red line? What it actually holds is the address of an object from the memory and not an image url. This is happening because you are calling toString() method on the downloadUrl object. So the way you are get the donwload url is not correct. This is the correct way for getting the url. Once you get url of the image and you add it to the database and everything will work as expected.



回答2:

I also had this problem and solved the problem as follows

RequestOptions options = new RequestOptions()
       .fitCenter()
       .error(errorImageDrawble)
       .diskCacheStrategy(DiskCacheStrategy.ALL)
       .priority(priority.HIGH);

Glide
     .with(applicationContext)
     .load(mProfileImageUrl)
     .apply(options)
     .into(mProfileImage);


回答3:

This is the function in which I updated the imagePath listener code which works fine now-

private fun saveUserInformation(){
        mName=mNameField.text.toString()
        mPhone=mPhoneField.text.toString()
        val userInfo=HashMap<String,Any>()
        userInfo.put("name",mName)
        userInfo.put("phone",mPhone)
        mCustomerDatabase.updateChildren(userInfo)
        if(resultUri!=null){
            val imagePath:StorageReference=mStorage.child("customer_profile_image").child(userId).child(resultUri.lastPathSegment )
            val uploadTask:UploadTask=imagePath.putFile(resultUri)

            uploadTask.addOnSuccessListener(OnSuccessListener <UploadTask.TaskSnapshot>(){taskSnapshot->

 //*********************This is the upadated part of code                   imagePath.downloadUrl.addOnSuccessListener(OnSuccessListener<Uri>{resultUri->
                        val downloadUrl=resultUri.toString()
                        val newImage= HashMap<String,Any>()
                        newImage["profileImageUrl"] = downloadUrl
                        mCustomerDatabase.updateChildren(newImage)
                    }).addOnFailureListener(OnFailureListener() {
                        Toast.makeText(this,"Profile image not",Toast.LENGTH_SHORT).show()
                    })
//*******************************************************************************


            }).addOnFailureListener(OnFailureListener() {
                Toast.makeText(this,"Profile image not uploaded",Toast.LENGTH_SHORT).show()
            })
        }
        finish()
    }