大小为 [0x0] 的 null 类 com.bumptech.glide.load.engine.GlideException 加载失败:收到空模型

问题描述 投票:0回答:3

我正在尝试加载我在 firebase 上保存的照片,它已保存成功,但无法再次加载图像,错误是

“对于大小为 [0x0] 的空值,加载失败 com.bumptech.glide.load.engine.GlideException 类:收到空模型”

我在代码中使用简单的行

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    currentUser?.let{user ->
        Glide.with(this)
            .load(user.photoUrl)

            .into(image_view)

我在 Build.Gradle 上的实现

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-firestore:21.4.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.navigation:navigation-fragment:2.2.1'
implementation 'androidx.navigation:navigation-ui:2.2.1'
implementation 'com.google.firebase:firebase-storage:19.1.1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.1'
implementation 'com.github.bumptech.glide:glide:4.8.0'
kapt 'com.github.bumptech.glide:compiler:4.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

Gradle.Build 上应用的插件

apply plugin: 'kotlin-kapt'

感谢您未来的回答!

顺便说一句,这是我的片段的完整代码

类NotificationsFragment:Fragment() {

private lateinit var notificationsViewModel: NotificationsViewModel
private lateinit var imageUri: Uri
private val REQUEST_IMAGE_CAPTURE = 100
private val currentUser = FirebaseAuth.getInstance().currentUser
private val user = FirebaseDatabase.getInstance()



override fun onCreateView(

    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?):
        View? {
    notificationsViewModel =
        ViewModelProviders.of(this).get(NotificationsViewModel::class.java)
    return inflater.inflate(R.layout.fragment_notifications,container,false)



}




override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)



    currentUser?.let{user ->
        Glide.with(this)
            .load(user.photoUrl)
            .placeholder(R.drawable.ic_profilepicture_box_orange_24dp)
            .into(image_view)






    }




    image_view.setOnClickListener {
        takePictureIntent()

    }
    button_save.setOnClickListener{

    }


}

private fun takePictureIntent() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { pictureIntent ->
        pictureIntent.resolveActivity(activity?.packageManager!!)?.also {
            startActivityForResult(pictureIntent, REQUEST_IMAGE_CAPTURE)
        }

    }


}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == AppCompatActivity.RESULT_OK) {
        val imageBitmap = data?.extras?.get("data") as Bitmap

        uploadImageAndSaveUri(imageBitmap)
    }
}

private fun uploadImageAndSaveUri(bitmap: Bitmap) {

    val baos = ByteArrayOutputStream()
    val baos1 = ByteArrayOutputStream()
    val storageRef = FirebaseStorage.getInstance()


        .reference

        .child("pics/${FirebaseAuth.getInstance().currentUser!!.uid}")
        .child("uid" + ".JPEG")
        .child("uid" + ".PNG")
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos1)
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
    val image = baos.toByteArray()
    val image1 = baos1.toByteArray()

    val upload = storageRef.putBytes(image)
    val upload1 = storageRef.putBytes(image1)

    upload.addOnCompleteListener { uploadTask ->
        if (uploadTask.isSuccessful){
            storageRef.downloadUrl.addOnCompleteListener{urlTask ->
                urlTask.result?.let{
                    imageUri = it
                    Toast.LENGTH_SHORT

                }

            }

        }

    }

    upload1.addOnCompleteListener { uploadTask1 ->
        if (uploadTask1.isSuccessful){
            storageRef.downloadUrl.addOnCompleteListener{urlTask ->
                urlTask.result?.let{
                    imageUri = it




                }

            }
        }

    }






    progressbar_pic.visibility = View.VISIBLE
    upload.addOnCompleteListener { uploadTask ->
        progressbar_pic.visibility = View.INVISIBLE

        if (uploadTask.isSuccessful) {
            storageRef.downloadUrl.addOnCompleteListener { urlTask ->
                urlTask.result?.let {
                    imageUri = it
                    imageUri.toString()
                    image_view.setImageBitmap(bitmap)


                }
            }
        } else {
            uploadTask.exception?.let {
                Toast.makeText(activity, "Upload Failed", Toast.LENGTH_SHORT).show();

            }
        }

    }

}

}

我修复了它,我还必须添加一些代码并编辑一些代码,这就是修复,因此它将保存 URL 和照片。

private  fun uploadImagetoFirebaseStorage(){
    if (selectedPhotoUri == null) return
    val filename = UUID.randomUUID().toString()
    val ref = FirebaseStorage.getInstance().getReference("/images/$filename")

    ref.putFile(selectedPhotoUri!!)
        .addOnSuccessListener {
            Log.d("dsa","$it")
            ref.downloadUrl.addOnSuccessListener {
                it.toString()

                saveUsertoFirebaseDatabase(it.toString())
            }
        }

}

private fun saveUsertoFirebaseDatabase(profileImageUrl: String){
    val uid = FirebaseAuth.getInstance().uid ?: ""
    val ref = FirebaseDatabase.getInstance().getReference("/users/$uid")
    val user = User(uid,username_edittext_register.text.toString(),profileImageUrl)

    ref.setValue(user)
        .addOnSuccessListener {
            Log.d("","Saved!!!!!!")
        }


}

}

类用户(val uid:字符串,val用户名:字符串,val profileImageUrl:字符串)

然后我就像你通常使用的那样使用了 Glide 模块

currentUser?.let{user ->

        Glide.with(this)

            .load(user.photoUrl)

            .into(image_view)


    }
android kotlin imageview android-glide appglidemodule
3个回答
0
投票

确保您在 firebase 数据库中输入了相同的名称,例如-我正在尝试在我的应用程序中发送图像,在我的模型中我声明了一个字符串“imageUrl”,但在 firebase 存储中我给出了名称“image”,确保两个名称相同......这将解决问题


0
投票

最常见的问题位于:

Glide.with(this)
     .load(user.photoUrl)
     .into(image_view)

user.photoUrl
image_view
为空。请使用
println()
进行检查。


0
投票

确保您的视图未设置为

View.GONE
,这就是 Glide 将其视为 0x0 的原因。

© www.soinside.com 2019 - 2024. All rights reserved.