Why android File.delete() , File.rename() fails on some devices? -
it works on nexus 5,but not working on samsung devices , oem devices.
this code:
file f = new file(path); if (f.exists()) { if (f.delete()) { mediascannerconnection.scanfile(ctx, new string[]{path, ""}, null, null); } else { // log.e(tag, ctx.getstring(r.string.unabletodelete)); } } else { toast.maketext(ctx,ctx.getstring(r.string.filenotfound),toast.length_short).show(); }
you need ask user if accept read/write permission
if (activitycompat.checkselfpermission(this, manifest.permission.write_external_storage) != packagemanager.permission_granted) { activitycompat.requestpermissions(this, new string[]{ manifest.permission.read_external_storage, manifest.permission.write_external_storage,}, 1); } else { createtemporaryfile(); }
don't forget add
<uses-permission android:name="android.permission.read_external_storage" />
i got issue device on api > 24 using file need disable uriexposure
if(build.version.sdk_int>=24){ try{ method m = strictmode.class.getmethod("disabledeathonfileuriexposure"); m.invoke(null); }catch(exception e){ e.printstacktrace(); } }
Comments
Post a Comment