保存后未显示个人资料图片(仅显示空白屏幕)

时间:2019-02-04 07:54:41

标签: android firebase android-studio firebase-realtime-database kotlin

我正在尝试上传用户个人资料图像并存储在firebase中。我的图像成功存储在firebase存储中和用户数据库中。

当我上传图像时,它会显示在屏幕上,但是一旦我保存并重新打开设置以查看个人资料图像,它就不会显示,只会显示空白的白色屏幕(但是重新打开时会显示名称和电话,仅图片未显示)

我已经附上了从db获取值并在ui中显示的代码,还添加了saveUserInformation(),当用户单击“保存图像”按钮时该调用将被调用,并且此函数将名称,电话和profileimage保存在db中。我在下面添加了数据库的屏幕快照,还添加了我遇到的错误摘要。

我在做什么错了?

这是我在控制台上记录的滑行输出: 个人资料img:目标:android.support.v7.widget.AppCompatImageView {528bbb2 V.ED..C .. ...... ID 0,0-300,300#7f0a00b9 app:id / profileImage} [CDS] rx超时: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

3 个答案:

答案 0 :(得分:0)

我也遇到了这个问题,并解决了以下问题

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

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

答案 1 :(得分:0)

您遇到以下错误:

  

com.google.android.gms.tasks.zzu@c0645c1的加载失败

因为您正在尝试显示不存在的图像。正如我在您的数据库中看到的那样,profileImageUrl属性包含一个无效有效的图片网址。

enter image description here

看到红线了吗?它实际持有的是内存中对象的地址,而不是图像的URL。发生这种情况是因为您正在toString()对象上调用downloadUrl方法。因此,获取下载URL的方法不正确。 This 是获取网址的正确方法。一旦获得图像的url并将其添加到数据库,一切将按预期工作。

答案 2 :(得分:0)

这是我更新imagePath侦听器代码的功能,现在可以正常工作了-

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