Delete file on SD card on Android 4.4 -


it known if have on android 4.4.4 file on sd card in folder not include app package name file cannot deleted.

is there way delete such file?

nasty workaround exists (see code below). tested on samsung galaxy s4, fix does't work on devices. wouldn’t count on workaround being available in future versions of android.

there great article explaining (4.4+) external storage permissions change.

you can read more workaround here. workaround source code this site.

public class mediafilefunctions  {     @targetapi(build.version_codes.honeycomb)     public static boolean deleteviacontentprovider(context context, string fullname)      {        uri uri=getfileuri(context,fullname);         if (uri==null)        {          return false;       }        try        {           contentresolver resolver=context.getcontentresolver();            // change type image, otherwise nothing deleted           contentvalues contentvalues = new contentvalues();           int media_type = 1;           contentvalues.put("media_type", media_type);           resolver.update(uri, contentvalues, null, null);            return resolver.delete(uri, null, null) > 0;        }        catch (throwable e)        {           return false;        }     }     @targetapi(build.version_codes.honeycomb)    private static uri getfileuri(context context, string fullname)     {       // note: check outside class whether os version >= 11        uri uri = null;        cursor cursor = null;        contentresolver contentresolver = null;        try       {           contentresolver=context.getcontentresolver();           if (contentresolver == null)             return null;           uri=mediastore.files.getcontenturi("external");           string[] projection = new string[2];           projection[0] = "_id";           projection[1] = "_data";           string selection = "_data = ? ";    // avoids sql injection           string[] selectionparams = new string[1];           selectionparams[0] = fullname;           string sortorder = "_id";           cursor=contentresolver.query(uri, projection, selection, selectionparams, sortorder);            if (cursor!=null)           {              try              {                 if (cursor.getcount() > 0) // file present!                 {                      cursor.movetofirst();                    int datacolumn=cursor.getcolumnindex("_data");                    string s = cursor.getstring(datacolumn);                    if (!s.equals(fullname))                       return null;                    int idcolumn = cursor.getcolumnindex("_id");                    long id = cursor.getlong(idcolumn);                    uri= mediastore.files.getcontenturi("external",id);                 }                 else // file isn't in media database!                 {                      contentvalues contentvalues=new contentvalues();                    contentvalues.put("_data",fullname);                    uri = mediastore.files.getcontenturi("external");                    uri = contentresolver.insert(uri,contentvalues);                 }              }              catch (throwable e)              {                 uri = null;              }                         {                 cursor.close();             }          }        }        catch (throwable e)        {           uri=null;        }        return uri;     }  } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -