0

In my app when user press on an 'ImageView' i am creating a chooser intent that will promote the user to change the picture.

Here is the code i use when user click on the ImageView

userProfileIMG.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            photoPickerIntent.setType("image/*");

            Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePhotoIntent.resolveActivity(getPackageManager()) != null)
            {
                try
                {
                    photoFile = Create_ImageFile();
                }
                catch (Exception e)
                {
                    Log.e("file", e.toString());
                }
                if (photoFile != null)
                {
                    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile.getAbsoluteFile()));
                }
            }

            String pickTitle = getString(R.string.select_or_take_new_picture);
            Intent chooserIntent = Intent.createChooser(photoPickerIntent, pickTitle);
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePhotoIntent});

            startActivityForResult(chooserIntent, ACTIVITY_SELECT_PICTURE);
        }
    });

And this is the result

enter image description here

My problem is when user press on 'Gallery' and select an image on 6.0 its giving security exception because i want to request the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> only if he pressed 'Gallery' is there is any way to override the behavior when user press 'Gallery' so that i request the permission then if he granted i let him to continue to the gallery else i deny him? Or i have to build the same chooser manually not depending on the system to build the dialog for me?

Mohammad Haidar
  • 1,109
  • 1
  • 16
  • 35
  • camera requires permission. you need to request the permission to access camera dynamically. – Swaminathan V Sep 30 '16 at 09:32
  • Better approach is to ask for permission when user enters Profile page. Show him the dialog to choose options only if the Camera and EXTERNAL_STORAGE permissions are granted. – Febi M Felix Sep 30 '16 at 09:35
  • @RaguSwaminathan Nope i am not accessing the camera, i am letting the camera itself to take the picture and that do not requires permission. In fact this is the way that you escape from permissions you let the system apps do the work for you. More here https://developer.android.com/training/permissions/best-practices.html – Mohammad Haidar Sep 30 '16 at 09:35
  • @FebiMathew i am letting this approach as my last choice. – Mohammad Haidar Sep 30 '16 at 09:37

1 Answers1

0

Writing and reading from external storage in Android 6.0 and above requires dynamically requesting permission. This is a special feature introduced in android 6.0 onwards [ Marshmallow ].

Though you declared the

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

in android manifest, when running in the android 6.0 will check if there is permission granted for your app to write to external storage. [ See app permissions detail in settings-> apps -> youapp -> permissions]. If it is disabled you might have faced the crash.

and for more details check the android developer portal

Also refer the below posts that will give you some idea.

Storage permission error in Marshmallow

Android 6.0 Marshmallow. Cannot write to SD Card

Update : Accessing through intent also requires to implement the permission system. Image from Gallery in Android 6(Marshmallow)

Let me know for queries.

Community
  • 1
  • 1
Swaminathan V
  • 4,663
  • 2
  • 22
  • 32
  • I am aware of that and my question has nothing to do with how to request a permission in Android 6.0 But instead am asking how to detect when user press 'Gallery' so than i can ask the permession. – Mohammad Haidar Sep 30 '16 at 09:43
  • Yes am attempting and i know that it needs to request a run-time permission on 6.0, But my question is not how to request the permission to access the device storage. My question is another different topic please read my question well and what i am requesting for – Mohammad Haidar Sep 30 '16 at 09:50
  • 1
    there is no way you can track when user clicks on gallery ? unless you go with custom dialog to show them. sorry for misunderstanding. – Swaminathan V Sep 30 '16 at 09:52
  • No worries my friend, yeah i guess this is the only choice i have. I will vote up your last comment for your effort. – Mohammad Haidar Sep 30 '16 at 09:54