保存后不显示个人资料图像(它只显示空白屏幕)

Posted

技术标签:

【中文标题】保存后不显示个人资料图像(它只显示空白屏幕)【英文标题】:Profile image not showing after saving(it just shows blank white screen) 【发布时间】:2019-06-27 22:49:16 【问题描述】:

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

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

我附加了从 db 获取值并在 ui 中显示的代码,还添加了 saveUserInformation(),当用户单击保存图像按钮时调用该函数,此函数将名称、电话和 profileimage 保存在 db 中。我在下面添加了数据库的屏幕截图,还添加了我遇到的错误的 sn-p。

我做错了什么?

这是我控制台记录时 glide 输出的内容: profile img here:目标:android.support.v7.widget.AppCompatImageView528bbb2 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

【问题讨论】:

请同时添加用于将profileImageUrl 添加到数据库的代码。 我已经更新了问题并添加了用于将 profileImageUrl 添加到数据库的代码 【参考方案1】:

您收到以下错误:

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

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

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

【讨论】:

我按照你说的修改了我的代码,是下载地址的问题,现在可以了。非常感谢。【参考方案2】:

我也遇到了这个问题,解决方法如下

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

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

【讨论】:

我尝试添加 requestoptions,但它不起作用,我仍然看不到任何图像,并且遇到了与我在上面添加的问题相同的错误。【参考方案3】:

这是我更新 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()
    

【讨论】:

以上是关于保存后不显示个人资料图像(它只显示空白屏幕)的主要内容,如果未能解决你的问题,请参考以下文章

使用 CURL 保存图像,有时它会保存空白图像

如何在统一引擎中从 Facebook API 获取用户名和用户个人资料图片?

从图库中选择图像后出现空白屏幕

相机屏幕仅显示空白黑色图像

在.net core 6的导航栏中显示用户个人资料图片[关闭]

swift 3中的按钮背景图像