【发布时间】:2021-07-15 17:28:27
【问题描述】:
我是 Kotlin 和 Android Studio 的新手,在从相机中检索图片时遇到了问题。
onActivityResult 中的 Intent 始终为 null,因此不会显示任何图像。当我删除数据时!==null,我得到以下错误
将结果 ResultInfo{who=null, request=1, result=-1, data=null} 传递给活动失败
这是我的代码:
MainActivity.kt:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.startButton.setOnClickListener{
dispatchTakePictureIntent()
}
}
val REQUEST_IMAGE_CAPTURE = 1
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) {
// Error occurred while creating the File
Log.d("ERROR", "An error occured")
null
}
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
this,
"pim.android.photoapp.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Log.d("DATA", data.toString())
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data !== null) {
val imageBitmap = data.extras?.get("data") as Bitmap
binding.imageView.setImageBitmap(imageBitmap)
}
}
lateinit var currentPhotoPath: String
@Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"JPEG_${timeStamp}_", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
).apply {
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = absolutePath
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pim.android.photoapp">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<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/Theme.PhotoApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="pim.android.photoapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
</manifest>
@xml/file_paths:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
</paths>
有人知道我在这里做错了什么吗?任何答案将不胜感激
【问题讨论】:
-
ACTION_IMAGE_CAPTURE不应返回Intent。您的照片应位于EXTRA_OUTPUT中指定的位置。 -
@CommonsWare 你的意思是我必须将 putExtra 中的 EXTRA_OUTPUT 更改为 ACTION_IMAGE_CAPTURE?因为那有点用,只有图片不会很清晰
-
"您的意思是我必须将 putExtra 中的 EXTRA_OUTPUT 更改为 ACTION_IMAGE_CAPTURE?" ——你已经有了。不过,您没有在
onActivityResult()中使用它。我之前的评论并不清楚:当您使用EXTRA_OUTPUT时,不要假设ACTION_IMAGE_CAPTURE会返回Intent,并且从不在@ 中寻找data额外987654332@。这就是你现在正在做的事情。相反,在onActivityResult()中,使用应写入您在EXTRA_OUTPUT中请求的位置的照片。
标签: android android-studio kotlin android-camera onactivityresult