Android 6.0 Marshmallow. Cannot write to SD Card -


i have app uses external storage store photographs. required, in manifest, following permissions requested

<uses-permission android:name="android.permission.camera" /> <uses-permission android:name="android.permission.write_external_storage" /> 

and uses following retrieve required directory

file sddir = environment             .getexternalstoragepublicdirectory(environment.directory_pictures);  simpledateformat dateformat = new simpledateformat("mm-dd", locale.us); string date = dateformat.format(new date()); storagedir = new file(sddir, getresources().getstring(             r.string.storagedir)             + "-" + date);  // create directory, error handling if (!storagedir.exists() && !storagedir.mkdirs()) {  ... fails here 

the app works fine on android 5.1 2.3; has been on google play on year.

following upgrade of 1 of testing phones (android one) 6, it's returning error when trying create requisite directory, "/sdcard/pictures/myapp-yy-mm".

the sd card configured "portable storage". i've formatted sd card. i've replaced it. i've rebooted. no avail.

also, built-in android screenshot functionality (via power+lower volume) failing "due limited storage space, or isn't allowed app or organisation".

any ideas?

i faced same problem. there 2 types of permissions in android:

  • dangerous (access contacts, write external storage...)
  • normal

normal permissions automatically approved android while dangerous permissions need approved android users.

here strategy dangerous permissions in android 6.0

  1. check if have permission granted
  2. if app granted permission, go ahead , perform normally.
  3. if app doesn't have permission yet, ask user approve
  4. listen user approval in onrequestpermissionsresult

here case: need write external storage.

first, check if have permission:

... private static final int request_write_storage = 112; ... boolean haspermission = (contextcompat.checkselfpermission(activity,             manifest.permission.write_external_storage) == packagemanager.permission_granted); if (!haspermission) {     activitycompat.requestpermissions(parentactivity,                 new string[]{manifest.permission.write_external_storage},                 request_write_storage); } 

then check user's approval:

@override public void onrequestpermissionsresult(int requestcode, string[] permissions, int[] grantresults) {     super.onrequestpermissionsresult(requestcode, permissions, grantresults);     switch (requestcode)     {         case request_write_storage: {             if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted)             {                 //reload activity permission granted or use features required permission             } else             {                 toast.maketext(parentactivity, "the app not allowed write storage. hence, cannot function properly. please consider granting permission", toast.length_long).show();             }         }     }  } 

you can read more new permission model here: https://developer.android.com/training/permissions/requesting.html


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -