Flutter 图像选择器明确请求许可

Noo*_*per 10 android flutter

图像选择器包说

无需配置 - 该插件应该开箱即用。

不再需要将 android:requestLegacyExternalStorage="true" 作为属性添加到 AndroidManifest.xml 中的标记,因为 image_picker 已更新为使用范围存储。

从图库中读取图像。所以我想我需要征求用户的一些许可,因为 Playstore 还说这个新包正在工作,不需要任何许可。我需要明确询问什么权限而且我不想将其保存在任何外部目录中我只想将图像上传到 firebase 存储编辑:图像选择器没有询问用户的任何权限这是错误的

Nit*_*ish 12

在android中读取和写入文件需要权限。
需要将这些权限添加到您的 AndroidManifest.xml 中

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

在您的场景中,您不需要执行任何操作,因为这已经由库
https://pub.dev/packages/image_picker处理

上述库不会将图像保存在外部存储中。

注意:使用相机拾取的图像和视频会保存到应用程序的本地缓存中,因此应该只是暂时存在。如果您需要永久存储所选图像,您有责任将其移动到更永久的位置。

有关更多信息,您可以参考此链接
https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media#accessing-stored-media

更新:image_picker Android 内部如何处理图像选取

对于画廊选择,它在内置文件选择器意图中打开,使用ACTION_GET_CONTENT关于操作获取内容

使用ACTION_GET_CONTENT- 打开文件时 - 由于用户参与选择应用程序可以访问的文件或目录,因此此机制不需要任何系统权限。您可以在 google 文档中阅读有关何时需要许可以及何时不需要许可的更多信息

Intent pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  pickImageIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
pickImageIntent.setType("image/*");

activity.startActivityForResult(pickImageIntent, REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY);
Run Code Online (Sandbox Code Playgroud)

并将结果 URI 复制到缓存目录的临时文件中并返回路径

     String extension = getImageExtension(context, uri);
      inputStream = context.getContentResolver().openInputStream(uri);
      file = File.createTempFile("image_picker", extension, context.getCacheDir());
      file.deleteOnExit();
      outputStream = new FileOutputStream(file);
      if (inputStream != null) {
        copy(inputStream, outputStream);
        success = true;
      }
 
Run Code Online (Sandbox Code Playgroud)

对于相机android.permission.CAMERA库,请求用户的相机权限并将相机图像保存在应用程序缓存目录中。

private void handleCaptureImageResult(int resultCode) {
    if (resultCode == Activity.RESULT_OK) {
      fileUriResolver.getFullImagePath(
          pendingCameraMediaUri != null
              ? pendingCameraMediaUri
              : Uri.parse(cache.retrievePendingCameraMediaUriPath()),
          new OnPathReadyListener() {
            @Override
            public void onPathReady(String path) {
              handleImageResult(path, true);
            }
          });
      return;
    }

    // User cancelled taking a picture.
    finishWithSuccess(null);
  }
Run Code Online (Sandbox Code Playgroud)

此代码按照image_picker: ^0.8.4+4其 github 页面上的版本代码 -图像选择器代码