我正在尝试将图片从手机图库上传到Firebase,但模拟器会一直关闭,并且没有记录任何错误。这是我的代码
val PICK_IMAGE_CODE=123
fun loadImage(){
var intent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,PICK_IMAGE_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode==PICK_IMAGE_CODE && data!=null && resultCode == RESULT_OK){
val selectedImage=data.data
val filePathColum= arrayOf(MediaStore.Images.Media.DATA)
val cursor= contentResolver.query(selectedImage,filePathColum,null,null,null)
cursor.moveToFirst()
val coulomIndex=cursor.getColumnIndex(filePathColum[0])
val picturePath=cursor.getString(coulomIndex)
cursor.close()
uploadImage(BitmapFactory.decodeFile(picturePath))
}
}
var DownloadURL:String?=""
fun uploadImage(bitmap:Bitmap){
adapter!!.notifyDataSetChanged()
var currentUser = mAuth!!.currentUser
val storage= FirebaseStorage.getInstance()
val storgaRef=storage.reference
val df= SimpleDateFormat("ddMMyyHHmmss")
val dataobj= Date()
val imagePath= currentUser!!.uid+ "."+ df.format(dataobj)+ ".jpg"
val ImageRef=storgaRef.child("imagePost/"+imagePath )
val baos= ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos)
val data= baos.toByteArray()
val uploadTask=ImageRef.putBytes(data)
uploadTask.addOnFailureListener{
Toast.makeText(applicationContext,"fail to upload", Toast.LENGTH_LONG).show()
}.addOnSuccessListener { taskSnapshot ->
DownloadURL= taskSnapshot.downloadUrl!!.toString()
ListTweets.removeAt(0)
adapter!!.notifyDataSetChanged()
}
}
如果我注释掉我对uploadImage的调用,那么模拟器不会崩溃,所以我认为我得到picturePath的方式正在发生,但我不确定。任何人都可以帮助我吗?
答案 0 :(得分:0)
我想通了,我没有要求权限,所以我使用的picturePath始终为null