我试图使用相机意图和EXTRA_OUTPUT用我的应用程序拍照。这在android 10和9上工作得很好(尽管我在10上使用了一个稍微不同的方法),但是当我尝试在android 8或7上运行它时,onActivityResult得到的结果代码是0.我使用的是android开发者页面上提供的例子 Take photos (联系)
private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
// Ensure that there's a camera activity to handle the intent
takePictureIntent.resolveActivity(packageManager)?.also {
// Create the File where the photo should go
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
null
}
// Continue only if the File was successfully created
photoFile?.also {
imageUri = FileProvider.getUriForFile(
applicationContext,
"com.example.gpscamera.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
}
}
}
}
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
imageName = "IMG$timeStamp"
val storageDir: File? = File(getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/GPSCamera")
//Log.d("path",storageDir.toString())
return File(
storageDir,
"$imageName.jpg"
).apply {
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = absolutePath
}
}
系统确实会抛出一个错误。
2020-05-06 17:05:05.584 6619-6619/? E/CAM_StateSavePic: exception while saving result to URI: Optional.of(content://com.example.gpscamera.fileprovider/GPSCamera/GPSCamera/IMG20200506_170337.jpg)
java.io.FileNotFoundException: No such file or directory
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:621)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1000)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:742)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:718)
at com.android.camera.captureintent.state.StateSavingPicture.onEnter(StateSavingPicture.java:84)
at com.android.camera.captureintent.stateful.StateMachineImpl.jumpToState(StateMachineImpl.java:62)
at com.android.camera.captureintent.stateful.StateMachineImpl.processEvent(StateMachineImpl.java:110)
at com.android.camera.captureintent.state.StateOpeningCamera$9.onClick(StateOpeningCamera.java:307)
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)
但在android 9上不会发生这种情况(使用的是相同的代码)。我错过了什么,导致它在android 7和8上无法工作?
原来是由于我试图放置文件的文件夹还不存在而导致的错误。改进后的代码可以在下面找到。
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
imageName = "IMG$timeStamp"
val storageDir: File? = File(getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/GPSCamera")
//Log.d("path",storageDir.toString())
return File(
storageDir,
"$imageName.jpg"
).apply {
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = absolutePath
// Check if parent folder exists, if not create the folder
if (!parentFile.exists()){
parentFile.mkdirs()
}
}
}