在Android 4.4上删除SD卡上的文件

时间:2014-03-10 17:26:17

标签: android-sdcard android-4.4-kitkat

众所周知,如果您在Android 4.4.4上的文件夹中的SD卡上没有包含应用程序包名称的文件,则该文件无法删除。

有没有办法删除这样的文件?

3 个答案:

答案 0 :(得分:1)

令人讨厌的解决方法存在(请参阅下面的代码)。在三星Galaxy S4上测试过,但此修复程序无法在所有设备上运行。此外,不会指望此解决方法可用于未来版本的Android。

great article explaining (4.4+) external storage permissions change

您可以阅读more about workaround here。 解决方法源代码来自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 to image, otherwise nothing will be 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 this class whether the OS version is >= 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 = ? ";    // this 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 the 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; 
            }
            finally
            {
                cursor.close();
            }
         } 
      } 
      catch (Throwable e) 
      { 
         uri=null; 
      } 
      return uri; 
   } 
}

答案 1 :(得分:0)

可以通过root或漏洞(在某些设备上可能无法正常工作):

http://www.androidpolice.com/2014/04/06/external-blues-redux-apps-still-have-a-loophole-for-writing-to-the-sd-card-on-kitkat-but-for-how-long/

可悲的是,谷歌宣布它不再是允许的了。

答案 2 :(得分:0)

试用此代码

String folderSD = Environment.getExternalStorageDirectory().toString()+"/Pictures/neoadnEditGyM/";

    Cursor c = db.obtenerDatoEjercicio(selecID);
    String stringFoto1 = c.getString(6).toString();
    String stringFoto2 = c.getString(7).toString();
    File foto1 = new File(folderSD+stringFoto1);
    if (foto1.exists()){
        foto1.delete(); 
        Toast.makeText(this, stringFoto1+" deleted.", Toast.LENGTH_LONG).show();
    }
    File foto2 = new File (folderSD+stringFoto2);
    if(foto2.exists()){
        foto2.delete();
        Toast.makeText(this, stringFoto2+" deleted.", Toast.LENGTH_LONG).show();
    }