【问题标题】:Take picture with camera in android 10在android 10中用相机拍照
【发布时间】:2020-04-23 20:04:36
【问题描述】:

随着 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 scoped storage 怎么做?

【问题讨论】:

标签: android android-camera android-10.0


【解决方案1】:

这行是做什么的:it.putExtra("return-data", true)

没有,通常。可能有一些相机应用程序在收到此未记录且(通常)不受支持的 Intent 额外时会执行某些操作。

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

他确实做到了,在前面的步骤中。最后,他想使用 uCrop 来允许用户裁剪图像。也许他想同时保留原始照片和裁剪后的照片。

【讨论】:

  • 那么,他所说的“//我们不能直接访问图像所以我们再次在不同的位置创建一个新文件并将其用于进一步处理”是什么意思
  • @ahmedosama:我不知道。坦率地说,我不认为这是一篇非常好的 Medium 帖子。如果您想要一个示例应用程序,this one of mine 有点旧但可能会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
  • 2011-10-07
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
相关资源
最近更新 更多