Skip to content Skip to sidebar Skip to footer

Image Save To Sdcard For 6.0.1 Android Version

This code works correctly under 6.0.1 android version but if i run this application on 6.0.1 android devices , it will not save images to sd card. What i need to update for 6.0.1 d

Solution 1:

On Android 6.0+, you need to request runtime permission to write to external storage.


In order to request runtime permission to write to external storage:

publicclassMarshmallowPermission {
    publicstaticfinalintEXTERNAL_STORAGE_PERMISSION_REQUEST_CODE=2;

    publicMarshmallowPermission() {
    }

    publicbooleancheckPermissionForExternalStorage(Activity activity) {
        if(Build.VERSION.SDK_INT >= 23) {
            intresult= ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if(result == PackageManager.PERMISSION_GRANTED) {
                returntrue;
            } else {
                returnfalse;
            }
        } else {
            returntrue;
        }
    }

    publicvoidrequestPermissionForExternalStorage(Activity activity) {
        if(ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(activity,
                    "External Storage permission needed. Please allow in App Settings for additional functionality.",
                    Toast.LENGTH_LONG).show();
            // user has previously denied runtime permission to external storage
        } else {
            ActivityCompat.requestPermissions(activity,
                    newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        }
    }
}

Then you can do

if(!marshmallowPermission.checkPermissionForExternalStorage(this)) {
    marshmallowPermission.requestPermissionForExternalStorage(this);
} else {
    // can write to external
}

And

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == MarshmallowPermission.EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE) {
        if(marshmallowPermission.checkPermissionForExternalStorage(this)) {
            // can write to external
        } else {
            // runtime permission denied, user must enable permission manually
        }
    }
}

Solution 2:

Refer the following link, How to save the image to SD card on button Click android. and Saving image from image view to sd card : Android. For detailed tutorial, http://www.android-examples.com/save-store-image-to-external-storage-android-example-tutorial/

Post a Comment for "Image Save To Sdcard For 6.0.1 Android Version"