好的,是时候回报了。
所以你有 Mainfest 的权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
你也有你的提供者元数据
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
一个遗漏的细节是 (android:authorities applicationId) 你需要添加你自己的应用程序包名称。
所以你在res 文件夹下有xml 文件,正如我们在manifest 中提到的那样,你在它下面创建了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>
我们完成了第 1 部分的复制粘贴。现在在onCreate(savedInstanceState: Bundle?) 上方的活动中定义这些美女
val REQUEST_IMAGE_CAPTURE = 1
lateinit var currentPhotoPath: String
var cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
你可能想看看原始资源,但像往常一样缺少细节 Android 开发人员:link
另一个缺失的细节是packageName + ".fileprovider", 注意如果你有方法,你需要提供自己的包名。
// org android developers
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.message
null
}
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
this,
packageName + ".fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
}
createImageFile函数
// org android developers
@Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File = this!!.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
}
}
测试。
使用 onClick 事件调用您的 dispatchTakePictureIntent() 方法,确保允许权限
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMAGE_CAPTURE) {
var mBitmap_org = MediaStore.Images.Media.getBitmap(
this.getContentResolver(),
//Uri.parse(currentPhotoPath)
Uri.fromFile(File(currentPhotoPath))
);
//mImg_display?.setImageBitmap(mBitmap_org)
}
}
不检查数据,我们将通过 imagePath 获取。如果您正在检查Uri.parse(currentPhotoPath),请确保它是Uri.fromFile(File(currentPhotoPath))
现在你有了你的位图,有时间花其他几个小时/天来调整解码大小,保存。
还有一种保存tokken图像的方法,如果我需要tokken图像保存在画廊中,也许你可以帮我看看我应该把它放在哪里
// org android developers
private fun galleryAddPic() {
Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE).also { mediaScanIntent ->
val f = File(currentPhotoPath)
mediaScanIntent.data = Uri.fromFile(f)
sendBroadcast(mediaScanIntent)
}
}