在Android 8上使用相机而无需WRITE_EXTERNAL_STORAGE权限。

huangapple go评论70阅读模式
英文:

Use Camera on Android 8 without WRITE_EXTERNAL_STORAGE permission

问题

我正在尝试使我的应用程序捕获图像并将其发送到服务器,而无需存储在文件系统中。

如果我像这样操作:

intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, SOME_REQUEST_CODE);

onActivityResult中,我可以提取图像如下:

Bitmap photo = (Bitmap) data.getExtras().get("data");

但这只给我一个缩略图

从文档中看来,获取全高清图像的(一种?)方法之一是通过指示相机应用将图像存储在某个外部位置,通过添加

intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

然而,这显然需要WRITE_EXTERNAL_STORAGE权限。

考虑到我不需要将图像存储在文件系统中,因为我无论如何都要通过网络发送它,我认为请求额外的存储权限违反了“只请求所需权限”的原则。所以是否有可能以某种方式在内存中获取全高清图像并避免向用户请求存储权限?也许存在一些特殊的FileProvider路径类型适用于这种情况?

我找到了这个答案:https://stackoverflow.com/questions/32462725/capture-image-without-permission-with-android-6-0/37166704 但它没有使用FileProvider(我必须在Android 8上使用)并且似乎不适用于我的情况。

谢谢。

英文:

I am trying to make my app capture an image and send it over to the server, without storing in the filesystem.

If I do it like this:

intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, SOME_REQUEST_CODE);

In onActivityResult I can extract the image via

Bitmap photo = (Bitmap) data.getExtras().get("data");

but this gives me only a thumbnail.

From the documentation it seems that (one of the?) ways to get a full HD image is by instructing the camera app to store the image at some external location, by adding

intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

However this obviously requires WRITE_EXTERNAL_STORAGE

Considering I don't need the image to be stored in the filesystem, as I am sending it via network anyway, I believe asking for extra storage permission is against "ask only what you need" principle. So is there a possibility to somehow get a full hd image in memory and avoid asking the user for storage permission? Perhaps there exists some special FileProvider path type for such cases?

I have found this answer: https://stackoverflow.com/questions/32462725/capture-image-without-permission-with-android-6-0/37166704 but it doesn't use FileProvider (which I have to use on Android 8) and doesn't seem to be applicable to my case.

Thanks.

答案1

得分: 3

系统相机应用或第三方相机应用无法访问您的应用程序的内部存储,您的应用程序也无法访问其他相机应用程序的内部存储。因此,外部存储似乎是在您的应用程序和相机应用程序之间传递数据的唯一方法。

英文:

System camera app or third party camera apps cannot access your app's internal storage, neither can your app access other camera app's internal storage. Hence External Storage seems to be the only way to communicate data between your app and Camera app.

答案2

得分: 2

只需使用文件提供程序并保存到getFilesDir或getExternalFilesDir,您不需要WRITE权限或询问用户。

英文:

Just use a file provider and save to getFilesDir or getExternalFilesDir and you do not need WRITE permission or ask the user..

huangapple
  • 本文由 发表于 2020年1月7日 01:11:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定