Android 拍照时,意图始终为空
我是Kotlin和Android Studio的新手,在从相机检索照片时遇到了一个问题 onActivityResult中的Intent始终为空,因此不会显示任何图像。当我删除数据==null,我得到以下错误 向活动传递结果ResultInfo{who=null,request=1,result=-1,data=null}失败 这是我的密码: MainActivity.kt:Android 拍照时,意图始终为空,android,android-studio,kotlin,android-camera,onactivityresult,Android,Android Studio,Kotlin,Android Camera,Onactivityresult,我是Kotlin和Android Studio的新手,在从相机检索照片时遇到了一个问题 onActivityResult中的Intent始终为空,因此不会显示任何图像。当我删除数据==null,我得到以下错误 向活动传递结果ResultInfo{who=null,request=1,result=-1,data=null}失败 这是我的密码: MainActivity.kt: class MainActivity : AppCompatActivity() { private latei
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/文件路径:
<?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>
有人知道我做错了什么吗?任何答案都将不胜感激图像数据不会由onActivityResult上的intent返回,而是保存在创建的文件路径上(如果考虑到不同android版本中的权限方面,文件创建成功) 第一步。使photofile成为全局变量
private var photoFile: File? = null
第二步。使用glabal照片文件更新dispath方法
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
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) {
photoFile?.let {
val imageBitmap: Bitmap= BitmapFactory.decodeFile(it.absolutePath)
binding.imageView.setImageBitmap(imageBitmap)
}
}
}
ACTION\u IMAGE\u CAPTURE
不应返回意图。您的照片应该位于EXTRA\u OUTPUT
@commonware中指定的位置。您的意思是我必须将putExtra中的EXTRA\u OUTPUT更改为ACTION\u IMAGE\u CAPTURE吗?因为这是可行的,只有图片不会很清晰“你的意思是我必须将putExtra中的额外输出更改为ACTION\u IMAGE\u CAPTURE吗?”--你已经有了。但是,您没有在ActivityResult()中使用它。我在前面的评论中不清楚:当您使用EXTRA\u OUTPUT
时,不要假设ACTION\u IMAGE\u CAPTURE
将返回意图
,也不要在意图
中查找额外数据。这就是你现在正在做的。相反,在onActivityResult()
中,使用应该写入EXTRA\u OUTPUT
中请求的位置的照片。这很有效,谢谢!很高兴它成功了