james kim
james kim

Reputation: 11

required android.permission Read_External_storage, or grantUrlPermissions error

I think my permissions properly located on AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ganedu.intent">

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="23" />


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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>


        <activity android:name=".NextActivity"
            android:label="This is next_activity"/>
    </application>

</manifest>

I have "android.permission.READ_EXTERNAL_STORAGE" and "android.permission.WRITE_EXTERNAL_STORAGE" right after manifest.

but the error is keep occured.

Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/40 from pid=2473, uid=10073 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

and this is my MainActivity.java

public class MainActivity extends AppCompatActivity {

public static final int IMAGE_GALLERY_REQUEST = 20;
Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   }

public void gallery_open(View view) {
    Intent galleryPickerIntent = new Intent(Intent.ACTION_PICK);
    File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    String pictureDirectoryPath = pictureDirectory.getPath();
    Uri data = Uri.parse(pictureDirectoryPath);
    galleryPickerIntent.setDataAndType(data,"image/*");
    startActivityForResult(galleryPickerIntent, IMAGE_GALLERY_REQUEST);




}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent){
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    if(resultCode == Activity.RESULT_OK){
        if(requestCode ==  IMAGE_GALLERY_REQUEST) {
            onSelectedFromGalleryResult(imageReturnedIntent);
        }
    }

}

private void onSelectedFromGalleryResult(Intent data){
    Bitmap bm  = null;
    if(data != null){
        try{
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
        }catch(IOException e){
            e.printStackTrace();
        }
        //ivImage.setImageBitmap(bm);
        ImageView imgView = (ImageView)findViewById(R.id.ivImage);
        imgView.setImageBitmap(bm);


    }
}

Upvotes: 0

Views: 2723

Answers (2)

Prabha Karan
Prabha Karan

Reputation: 1329

You need to provide the run time permission for android marshmallow version

You can add this code in onCreate() or any click events, try this:

     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1){
            //    requestPermission();
            requestAppPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE },
                    R.string.app_name, REQUEST_PERMISSIONS);

            //this code will be executed on devices running ICS or later
        }

also add this function for requesting the run time permission

   public void requestAppPermissions(final String[] requestedPermissions,
                                          final int stringId, final int requestCode) {
            mErrorString.put(requestCode, stringId);
            int permissionCheck = PackageManager.PERMISSION_GRANTED;
            boolean shouldShowRequestPermissionRationale = false;
            for (String permission : requestedPermissions) {
                permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
                shouldShowRequestPermissionRationale = shouldShowRequestPermissionRationale || ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
            }
            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                if (shouldShowRequestPermissionRationale) {
                    Snackbar.make(findViewById(android.R.id.content), stringId,
                            Snackbar.LENGTH_INDEFINITE).setAction("GRANT",
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ActivityCompat.requestPermissions(DashBoardActivity.this, requestedPermissions, requestCode);
                                }
                            }).show();
                } else {
                    ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
                }
            } else {
                onPermissionsGranted(requestCode);
            }
        }

This function is called when the permission is granted and you can read the internal and external storage

public void onPermissionsGranted(final int requestCode) {
        Toast.makeText(this, "Permissions Received.", Toast.LENGTH_LONG).show();
    }

The below code for checking the requested permission result

  @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        int permissionCheck = PackageManager.PERMISSION_GRANTED;
        for (int permission : grantResults) {
            permissionCheck = permissionCheck + permission;
        }
        if ((grantResults.length > 0) && permissionCheck == PackageManager.PERMISSION_GRANTED) {
            onPermissionsGranted(requestCode);
        } else {
            Snackbar.make(findViewById(android.R.id.content), mErrorString.get(requestCode),
                    Snackbar.LENGTH_INDEFINITE).setAction("ENABLE",
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    }).show();
        }
    }

Upvotes: 1

Aditi
Aditi

Reputation: 389

James I suppose you are facing this error on Android 6.0 or onwards. You need to handle the permissions explicitly in these versions. Please find the android developer link

https://developer.android.com/guide/topics/permissions/requesting.html

Upvotes: 2

Related Questions