问题描述
我正在尝试加载保存在Firebase上的照片,保存成功,但是无法再次加载图像,错误是
I'm trying to load a photo I saved on firebase, It's saved successful but it can't get the image loaded again the error is
大小为[0x0]的null加载失败com.bumptech.glide.load.engine.GlideException类:收到的空模型"
"Load failed for null with size [0x0] class com.bumptech.glide.load.engine.GlideException: Received null model"
我在代码上使用了简单的行
I use the simple line on my code
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上的实现
My implementations on 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上应用的插件
Applied plugin on Gradle.Build
apply plugin: 'kotlin-kapt'
感谢您将来的回答!
顺便说一下,这是我完整的片段代码
BTW here's my entire code for the fragment
NotificationsFragment类:Fragment(){
class 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和照片.
I fixed it, I had to add a few more codes and edit some too, this was the fix so it would save the URL and the photo.
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!!!!!!")
}
}
}
class User(val uid:String,val username:String,val profileImageUrl:String)
class User(val uid: String, val username: String, val profileImageUrl: String)
然后我就像您通常使用的那样使用了Glide模块
then I just used Glide module as you usually use it
currentUser?.let{user ->
Glide.with(this)
.load(user.photoUrl)
.into(image_view)
}
推荐答案
确保您在firebase数据库中输入了相同的名称,例如-我正在尝试在我的应用程序中发送图像,在我的模型中我声明了一个字符串'imageUrl',但在Firebase存储中,我给了名称'image',请确保这两个名称相同....它将解决此问题
make sure that you have entered the same name in firebase database, e.g- I am trying to send image in my app, in my model I have declared a string 'imageUrl', but in firebase storage I gave the name 'image', make sure that the both names are same.... it will resolve the issue
这篇关于大小为[0x0]的null加载失败,类为com.bumptech.glide.load.engine.GlideException:收到的null模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!