当我单击gridview图像并按上载按钮以将图像上载到Firebase时,应用程序崩溃。发生崩溃是因为在上传之前将文件转换为Bitmap。我还授予了权限,并允许该应用访问内存(读和写)。
清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
代码:
fun getBitmap(imgUrl: String): Bitmap?
{
Log.d("check location", "Parameter: $imgUrl")
val imageFile = File(imgUrl)
Log.d("check lcoation", "File: ${imageFile.toString()}")
var fis: FileInputStream? = null
var bitmap: Bitmap? = null
try
{
fis = FileInputStream(imageFile)
bitmap = BitmapFactory.decodeStream(fis)
}
catch (e: FileNotFoundException)
{
Log.e(TAG, "getBitmap: FileNotFoundException: ${e.message}")
}
finally
{
try
{
fis!!.close()
}
catch (e: IOException)
{
Log.e(TAG, "getBitmap: FileNotFoundException: ${e.message}")
}
}
return bitmap
}
日志:
2019-10-30 /com.example.__ D/check location: Parameter: file:///storage/emulated/0/DCIM/camera/IMG_20191029_101626.jpg
2019-10-30 /com.example.__ D/check lcoation: File: file:/storage/emulated/0/DCIM/camera/IMG_20191029_101626.jpg
错误:
E/ImageManager: getBitmap: FileNotFoundException: file:/storage/emulated/0/DCIM/camera/IMG_20191029_101626.jpg (No such file or directory)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.__, PID: 15275
kotlin.KotlinNullPointerException
at com.example.__.utils.ImageManagerKt.getBitmap(ImageManager.kt:32)
at com.example.__.data.FirebaseUserPhoto.uploadNewPhoto(FirebaseUserPhoto.kt:35)
at com.example.__.viewModel.NextViewModel.uploadNewPhoto(NextViewModel.kt:48)
at com.example.__.ui.share.NextFragment$onCreateView$2.onClick(NextFragment.kt:59)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
答案 0 :(得分:1)
您imgUrl
中的val imageFile = File(imgUrl)
以file://
开头。
将其删除,因为File()
不需要以file://
开头的路径。