我正在关注https://developer.android.com/training/camera/photobasics.html
的示例我让它短暂工作,然后开始增强代码,现在拍照后,它不再存储在文件系统中。
private fun dispatchTakePictureIntent() {
val imageCaptureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val componentName = imageCaptureIntent.resolveActivity(packageManager)
// Ensure that there's a camera activity to handle the intent
if (componentName == null) {
Log.e("Ztuph", "componentName == null, cannot takePictureIntent.resolveActivity(packageManager))")
}
else try {
val photoFile = createImageFile()
val authority = "com.ztuph.android.FileProvider"
try {
photoUri = FileProvider.getUriForFile(this, authority, photoFile)
Log.d("Ztuph", "1 photoUri = $photoUri")
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
startActivityForResult(imageCaptureIntent, RequestCode.IMAGE_CAPTURE.ordinal)
Log.d("Ztuph", "startActivityForResult(imageCaptureIntent, RequestCode.IMAGE_CAPTURE.ordinal)")
}
catch (illegalArgumentException: IllegalArgumentException) {
// TODO
}
}
catch (ioException: IOException) {
// TODO Error occurred while creating the File
Log.e("Ztuph", "can't create file", ioException)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
when (requestCode){
RequestCode.IMAGE_CAPTURE.ordinal -> when (resultCode){
Activity.RESULT_CANCELED -> {
Log.d(TAG, "IMAGE_CAPTURE result canceled")
}
Activity.RESULT_OK -> {
Log.d(TAG, "onActivityResult: requestCode = IMAGE_CAPTURE, resultCode = RESULT_OK")
if (intent == null) {
Log.e(TAG, "intent == null")
val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
mediaScanIntent.data = photoUri
sendBroadcast(mediaScanIntent)
Log.d(TAG, "broadcast Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) with $photoUri")
}
else {
if (photoUri == null) Log.e(TAG, "photoUri == null")
else Log.d(TAG, "2 photoUri = $photoUri")
val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
mediaScanIntent.data = photoUri
sendBroadcast(mediaScanIntent)
Log.d(TAG, "broadcast Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) with $photoUri")
}
}
else -> {
Log.e(TAG, "Cannot handle resultCode = $resultCode")
}
}
else -> {
Log.e(TAG, "Cannot handle requestCode = $requestCode")
}
}
}
Logcat中似乎没有任何内容表明存在问题。
它在仿真器和真实设备(Galaxy Note 8)上的运行方式也不同。在真实设备上,它创建目录“SD卡> Android>数据> com.ztuph.android>文件>图片”但没有任何内容存储在那里。此外,onActivityResult中的参数意图为null。
在仿真(Nexus 5X API 27)上,我无法判断是否正在创建目录(“文件”应用程序太原始),但是没有文件了。昨天,我的应用程序正在创建image.jpg文件,今天它不是。此外,在模拟器上,onActivityResult中的参数意图不为空。
我的清单看起来像
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ztuph.android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="aia-compat-api-min-version"
android:value="1" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.ztuph.android.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
有没有人有任何疑难解答提示或其他想法,为什么图像捕获会停止写入文件系统?
答案 0 :(得分:1)
Logcat中似乎没有任何内容表明存在问题。
最有可能,但错误将来自相机应用程序,因为它无法写入您想要的位置。将FLAG_GRANT_WRITE_URI_PERMISSION
添加到您的Intent
。
它在仿真器和真实设备(Galaxy Note 8)上的运行方式也不同。
大约10,000个Android设备型号中有数百个预安装的相机应用程序。用户可能会安装数百种其他相机应用程序。每当您调用第三方应用程序时,您都可能会得到不同的结果。
此外,onActivityResult中的参数意图为null。
应该是null
。
我无法判断是否正在创建目录(“文件”应用程序太原始)
在Android Studio 3.0 +中使用设备文件资源管理器。