android代码删除sd卡文件在哪里,android - 如何从SD卡中删除文件?
Android 4.4及更改
除了特定于程序包的目錄外,不允許應用程序向外部存儲寫入(刪除,修改...)。
正如Android文檔所述:
“不得允許應用程序寫入輔助外部存儲?? 設備,除了允許的特定于包的目錄?? 合成權限。“
但是存在令人討厭的解決方法(參見下面的代碼)。 在三星Galaxy S4上測試過,但此修復程序無法在所有設備上運行。 此外,我不會指望在將來的Android版本中提供此解決方法。
有一篇很棒的文章解釋了(4.4+)外部存儲權限的變化。
您可以在此處詳細了解解決方法。解決方法源代碼來自此站點。
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;
}
}
總結
以上是生活随笔為你收集整理的android代码删除sd卡文件在哪里,android - 如何从SD卡中删除文件?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 支持M1芯片mac 达芬奇17中文版(详
- 下一篇: Mac插件分享——AE插件、PS插件、F