我正在尝试启动相机拍照,然后将其保存到图库中并恢复Uri,以将其显示为图像视图。
有了这个,稍后我将其发送到FirebaseStorage。
(我简化了变量和方法的名称,以提高可读性)
此刻,我执行了单击操作,以检查所需的权限。
button.setOnClickListener {
val permissions = arrayOf(
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
Permissions.check(context!!, permissions, null, null, object : PermissionHandler() {
override fun onGranted() {
dispatchTakePictureIntent()
}
})
}
private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
takePictureIntent.resolveActivity(context!!.packageManager)?.also {
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
Log.d("PictureIntent", "Error trying to get file")
null
}
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
context!!,
"com.elias.myapplication.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
activity!!.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
}
按照google的文档,在清单中,我像这样添加了FileProvider的声明:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
这是所有问题都来了...
在xml文件file_paths
中,如果我将路径放置为例如:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path
name="my_images"
path="/storage/emulated/0/Android/data/com.elias.myapplication/files/Pictures/" />
</paths>
我收到此错误:
Process: com.elias.myapplication, PID: 24093
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:605)
at androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:417)
at com.elias.myapplication.ui.main.MainFragment.dispatchTakePictureIntent(MainFragment.kt:76)
at com.elias.myapplication.ui.main.MainFragment.access$dispatchTakePictureIntent(MainFragment.kt:28)
at com.elias.myapplication.ui.main.MainFragment$onViewCreated$1$1.onGranted(MainFragment.kt:59)
at com.nabinbhandari.android.permissions.Permissions.check(Permissions.java:105)
at com.el.myapplication.ui.main.MainFragment$onViewCreated$1.onClick(MainFragment.kt:57)
at android.view.View.performClick(View.java:7333)
at android.widget.TextView.performClick(TextView.java:14160)
at android.view.View.performClickInternal(View.java:7299)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27773)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
在阅读有关SO的更多问题时,我找到了“解决方案”。使用以下命令修改xml文件:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
使用此解决方案不会崩溃,但是在onActivityResult
中,意图到达null
这是我迷路的地方。
我该如何解决?
答案 0 :(得分:0)
我收到此错误:
path
相对于元素名称中定义的基本位置。使用:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path
name="my_images"
path="Pictures" />
</paths>
但是,在onActivityResult中,意图到达null
应该为null
。您知道图像应该在哪里:photoFile
所标识的位置。在那寻找。有关将FileProvider
与ACTION_IMAGE_CAPTURE
一起使用的示例,请参见this sample app。