拍照时意图始终为空
Posted
技术标签:
【中文标题】拍照时意图始终为空【英文标题】:Intent is always null when taking a picture 【发布时间】:2021-07-15 17:28:27 【问题描述】:我是 Kotlin 和 android Studio 的新手,在从相机中检索图片时遇到了问题。
onActivityResult 中的 Intent 始终为 null,因此不会显示任何图像。当我删除数据时!==null,我得到以下错误
将结果 ResultInfowho=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
中请求的位置的照片。
【参考方案1】:
onActivityResult 上的 Intent 不会返回 Image 数据,而是保存在创建的文件路径上(如果文件创建成功,考虑到不同 android 版本的权限方面)
步骤 1. 将照片文件设为全局变量
private var photoFile: File? = null
第 2 步。使用 glabal photofile 更新您的 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)
第 3 步。更新您的活动结果
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_IMAGE_CAPTURE onActivityResult 的意图为空,数据为空
判断字符串为空为 null 为 whitespace 工具类