Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter image-picker explicitly ask permission

Imagepicker package says

No configuration required - the plugin should work out of the box.

It is no longer required to add android:requestLegacyExternalStorage="true" as an attribute to the tag in AndroidManifest.xml, as image_picker has been updated to make use of scoped storage.

reading images from gallery. so I think I need to ask some permission from the user as playstore also says this New package is just working and not asking for any permission. What permissions I need to explicitly ask And I don't want to save it on any external directory I just want to upload image to firebase storage Edit: image picker is not asking any permission from the user is this wrong

like image 732
Noobdeveloper Avatar asked Jan 24 '23 06:01

Noobdeveloper


1 Answers

Permission needed to read and write files in the android are.
These permission are required to be added to your AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

In your scenario, you don't need to do anything as this is already handled by the library
https://pub.dev/packages/image_picker

Above mentioned library doesn't save the image in external storage.

Note: Images and videos picked using the camera are saved to your application's local cache, and should therefore be expected to only be around temporarily. If you require your picked image to be stored permanently, it is your responsibility to move it to a more permanent location.

For more info you can refer to this link
https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media#accessing-stored-media

Update : How image picking is handled internally in image_picker for Android

For Gallery pick it opens in inbuild file picker intent using ACTION_GET_CONTENT(about action get content)

When opening file using ACTION_GET_CONTENT - Because the user is involved in selecting the files or directories that your app can access, this mechanism doesn't require any system permissions. You can read more about when permission is needed and when not in google docs

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);

and copies the result URI in temp file in cache directory and return the path

     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;
      }
 

For Camera library request the camera permission android.permission.CAMERA from the user and save the camera image in app cache directory.

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);
  }

This code is as per version image_picker: ^0.8.4+4 code present on their github page - image picker code

like image 153
Nitish Avatar answered Feb 01 '23 18:02

Nitish