Piy*_*mar 5 camera android kotlin
我试图使用 Android 设备中预装的本机相机应用程序来为我的应用程序捕获图像。为此,我一直在使用这段代码:
当用户点击按钮时调用一个函数
take_img_btn.setOnClickListener {
dispatchTakePictureIntent()
}
Run Code Online (Sandbox Code Playgroud)
该函数应该执行以下操作:
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
ex.printStackTrace()
null
}
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
this,
"com.example.android.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
}
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yy-MM-dd-HH-mm-ss-SS",Locale.getDefault()).format(System.currentTimeMillis())
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"FNL_${timeStamp}_",
".jpg",
storageDir /* directory */
).apply {
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = absolutePath
currentPhotoName = name
}
}
Run Code Online (Sandbox Code Playgroud)
FileProvider 在 Manifest 中配置如下
<application>
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.android.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>
Run Code Online (Sandbox Code Playgroud)
我还要求清单中的相机功能
<manifest>
...
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
...
</manifest>
Run Code Online (Sandbox Code Playgroud)
当我在 Android 设备(Realme 6 Pro)上运行此应用程序并点击按钮时:
- 该应用程序打开本机相机应用程序
- 我可以捕捉图像
- 图像保存到指定位置
但是,当这个相同的应用程序部署在 OnePlus 设备(在我们的例子中是 OnePlus Nord)上并点击按钮时,什么也不会发生。感觉这个按钮没有编程。
此代码取自此处: https: //developer.android.com/training/camera/photobasics
如果有人提供帮助,那将非常有帮助。
免责声明:我不擅长解释
正如@CommonsWare所建议的,这就是我所做的。
我基本上避免使用并按照自己的风格进行操作,而不是仅仅从Google 官方文档resolveActivity()中复制粘贴
这是我对相机应用程序的最终调用:
private fun dispatchTakePictureIntent() {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
ex.printStackTrace()
null
}
if (photoFile != null){
val photoURI: Uri = FileProvider.getUriForFile(
this,
"com.example.android.fileprovider",
photoFile
)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE)
}
}
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yy-MM-dd-HH-mm-ss-SS",Locale.getDefault()).format(System.currentTimeMillis())
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"FNL_${timeStamp}_",
".jpg",
storageDir /* directory */
).apply {
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = absolutePath
currentPhotoName = name
}
}
Run Code Online (Sandbox Code Playgroud)
最后,非常感谢@CommonsWare
| 归档时间: |
|
| 查看次数: |
3486 次 |
| 最近记录: |