Search code examples
androidpermissionsmanifestandroid-sdcardsave-image

No permission to WRITE on SD Card


I am desperatly looking for a mistake in my code and would really appreciate if sb could help me with that. I am trying to save an Image to the SD Card of my Samsung Galaxy Tab s2 but no matter what code I use, I do not seem to be able to make any changes on the SD Card.

This code passage below demonstrates that despite the changes in the manifest, I have no permission to write data on the SD card.

Is there anyone who had the same problem or knows what is going on here?

(android:minSdkVersion="19"; android:targetSdkVersion="25")

CODE:

    File filepath = Environment.getExternalStorageDirectory();
    File dir = new File(filepath.getAbsolutePath() + "/SaveImage");

    MediaStore.Images.Media.insertImage(getContentResolver(), bmp ,"Image.jpg", "IMAGE");

MANIFEST:

<manifest

xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ma.singhalja.saveimage">

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

<application

LOG:

03-28 01:16:54.647 14292-14292/com.ma.singhalja.saveimage E/MediaStore: Failed to insert image
                                                                    java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=14292, uid=10141 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
                                                                        at android.os.Parcel.readException(Parcel.java:1620)

Solution

  • You have to handle runtime permission for this,Take a look in this steps

    Copy this Class:

        public class PermissionHandler {
    
    
    // Run Time Permission List
            private static final String MNC = "MNC";
    
            // Calendar group.
            public static final String READ_CALENDAR = Manifest.permission.READ_CALENDAR;
            public static final String WRITE_CALENDAR = Manifest.permission.WRITE_CALENDAR;
    
            // Camera group.
            public static final String CAMERA = Manifest.permission.CAMERA;
    
            // Contacts group.
            public static final String READ_CONTACTS = Manifest.permission.READ_CONTACTS;
            public static final String WRITE_CONTACTS = Manifest.permission.WRITE_CONTACTS;
    
    
            // Location group.
            public static final String ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
            public static final String ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    
            // Microphone group.
            public static final String RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    
            // Phone group.
            public static final String READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
            public static final String CALL_PHONE = Manifest.permission.CALL_PHONE;
            public static final String READ_CALL_LOG = Manifest.permission.READ_CALL_LOG;
            public static final String WRITE_CALL_LOG = Manifest.permission.WRITE_CALL_LOG;
            public static final String ADD_VOICEMAIL = Manifest.permission.ADD_VOICEMAIL;
            public static final String USE_SIP = Manifest.permission.USE_SIP;
            public static final String PROCESS_OUTGOING_CALLS = Manifest.permission.PROCESS_OUTGOING_CALLS;
    
            // Sensors group.
            public static final String BODY_SENSORS = Manifest.permission.BODY_SENSORS;
            public static final String USE_FINGERPRINT = Manifest.permission.USE_FINGERPRINT;
    
            // SMS group.
            public static final String SEND_SMS = Manifest.permission.SEND_SMS;
            public static final String RECEIVE_SMS = Manifest.permission.RECEIVE_SMS;
            public static final String READ_SMS = Manifest.permission.READ_SMS;
            public static final String RECEIVE_WAP_PUSH = Manifest.permission.RECEIVE_WAP_PUSH;
            public static final String RECEIVE_MMS = Manifest.permission.RECEIVE_MMS;
            public static final String READ_CELL_BROADCASTS = "android.permission.READ_CELL_BROADCASTS";
    
            // Bookmarks group.
            public static final String READ_HISTORY_BOOKMARKS = "com.android.browser.permission.READ_HISTORY_BOOKMARKS";
            public static final String WRITE_HISTORY_BOOKMARKS = "com.android.browser.permission.WRITE_HISTORY_BOOKMARKS";
    
    
            public static boolean isPermissionGranted(Activity mContext, String Permission, String Text, int PermissionCode) {
    
                if (ContextCompat.checkSelfPermission(mContext, Permission) != PackageManager.PERMISSION_GRANTED) {
                    reqPermission(mContext, Text, PermissionCode, Permission);
                    return false;
                }
    
                return true;
            }
    
    
            public static void reqPermission(Activity mContext, String Text, int PermissionCode, String Permission) {
                if (!ActivityCompat.shouldShowRequestPermissionRationale(mContext, Permission)) {
                    ActivityCompat.requestPermissions(mContext, new String[]{Permission}, PermissionCode);
                } else {
                    openAlertDialog(mContext, Text);
                }
            }
    
            public static void openAlertDialog(final Context mContext, String Text) {
    
                new AlertDialog.Builder(mContext)
                        .setTitle("Permission")
                        .setMessage(mContext.getResources().getString(R.string.app_name) + " needs to access " + Text + " Permission for using this features from Setting >> Permissions.")
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // Your code
                                String packageName = "you.App.Id";
                                try {
                                    //Open the specific App Info page:
                                    Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                    intent.setData(Uri.parse("package:" + packageName));
                                    mContext.startActivity(intent);
    
                                } catch (ActivityNotFoundException e) {
                                    //e.printStackTrace();
                                    //Open the generic Apps page:
                                    Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                                    mContext.startActivity(intent);
    
                                }
    
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // do nothing
                            }
                        })
                        .show();
    
    
            }
    
        }
    

    Before you save image you need to check this

    if (PermissionHandler.isPermissionGranted((Activity) mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE, "External Storage", 1000)) {
                            // Save your Image code
                        }
    

    Thats it.Happy Coding :)