[在Android 10中使用相机拍照

问题描述 投票:0回答:1

有了android 10中与范围存储相关的所有更改,我们如何打开相机。我看到一些建议使用文件提供程序的教程,但是我不太了解。您可以提供一段代码来启动相机意图并接收所拍摄的图像吗?

编辑:在本文中:https://medium.com/@arkapp/accessing-images-on-android-10-scoped-storage-bbe65160c3f4他提供了此代码:

    fun takePicture(context: Activity, imageName: String) {try {
  val capturedImgFile = File(
      context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
      imageName)

  captureImgUri = FileProvider.getUriForFile(
      context, 
      context.applicationContext.packageName + ".my.package.name.provider",
      capturedImgFile)val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).also {

   it.putExtra(MediaStore.EXTRA_OUTPUT, captureImgUri)
   it.putExtra("return-data", true)
   it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
   context.startActivityForResult(it, REQUEST_CODE_TAKE_PICTURE)

  }
 } catch (e: ActivityNotFoundException) {e.printStackTrace()}
}@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 if (resultCode != RESULT_OK) {return}

 if (requestCode == REQUEST_CODE_TAKE_PICTURE) {

  /*We cannot access the image directly so we again create a new File at different location and use it for futher processing.*/

  Bitmap capturedBitmap = getBitmap(this, captureImgUri)

  /*We are storing the above bitmap at different location where we can access it.*/  val capturedImgFile = File(
   getExternalFilesDir(Environment.DIRECTORY_PICTURES), 
   getTimestamp() + "_capturedImg.jpg");  convertBitmaptoFile(capturedImgFile, capturedBitmap)/*We have to again create a new file where we will save the processed image.*/  val croppedImgFile = File(
    getExternalFilesDir(Environment.DIRECTORY_PICTURES), 
    getTimestamp() + "_croppedImg.jpg");  startCrop(
     this,
     Uri.fromFile(capturedImgFile),
     Uri.fromFile(croppedImgFile))}
 super.onActivityResult(requestCode, resultCode, data)
}

我有两个问题:

  • 此行的作用是:it.putExtra(“ return-data”,true)
  • 在OnActivityResulty中,为什么他不直接使用uri,他首先创建了一个文件,然后将其解析为uri,这是为了什么? android 10作用域存储该怎么做?
android android-camera android-10.0
1个回答
0
投票

此行的作用:it.putExtra(“ return-data”,true)

通常没有。可能有一些相机应用会在收到未记录且(通常)不受支持的Intent附加信息时执行某些操作。

在OnActivityResulty中,为什么他不直接使用uri

他做了,在较早的步骤中。最后,他希望使用uCrop允许用户裁剪图像。也许他想同时保留原始照片和裁剪后的照片。

© www.soinside.com 2019 - 2024. All rights reserved.