无法从存储中删除文件
Unable to delete file from storage
我正在尝试从 phone 存储中删除一个文件,但它总是返回错误。我已经尝试了堆栈上给出的大部分方法,但没有任何效果。
final File file = new File(path);
if (path != null) {
AlertDialog alertDialog = new AlertDialog
.Builder(getContext()).setTitle("Delete")
.setMessage("Are you sure you want to delete this video?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean fileDeleted = file.delete();
Toast.makeText(getContext(), String.valueOf(fileDeleted), Toast.LENGTH_SHORT).show();
mAdapter.notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.cancel, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
您可以试试这个代码:
public class FileUtils {
public static boolean deleteFile(File file, Context context) {
if (file == null || !file.exists()) return true;
file.delete();
if (file.exists()) {
try {
file.getCanonicalFile().delete();
} catch (IOException e) {
e.printStackTrace();
}
if (file.exists()) context.deleteFile(file.getName());
}
return !file.exists();
}
}
告诉我你传递给文件的文件路径,如果你把文件写入SD卡,你将无法删除它因为
从 Android 4.4 开始,应用程序无法再写入 micro SD 卡。
这是我尝试了很多方法后使用的代码。
private static boolean delete(final Context context, final File file) {
final String where = MediaStore.MediaColumns.DATA + "=?";
final String[] selectionArgs = new String[] {
file.getAbsolutePath()
};
final ContentResolver contentResolver = context.getContentResolver();
final Uri filesUri = MediaStore.Files.getContentUri("external");
contentResolver.delete(filesUri, where, selectionArgs);
if (file.exists()) {
contentResolver.delete(filesUri, where, selectionArgs);
}
return !file.exists();
}
我正在尝试从 phone 存储中删除一个文件,但它总是返回错误。我已经尝试了堆栈上给出的大部分方法,但没有任何效果。
final File file = new File(path);
if (path != null) {
AlertDialog alertDialog = new AlertDialog
.Builder(getContext()).setTitle("Delete")
.setMessage("Are you sure you want to delete this video?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean fileDeleted = file.delete();
Toast.makeText(getContext(), String.valueOf(fileDeleted), Toast.LENGTH_SHORT).show();
mAdapter.notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.cancel, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
您可以试试这个代码:
public class FileUtils {
public static boolean deleteFile(File file, Context context) {
if (file == null || !file.exists()) return true;
file.delete();
if (file.exists()) {
try {
file.getCanonicalFile().delete();
} catch (IOException e) {
e.printStackTrace();
}
if (file.exists()) context.deleteFile(file.getName());
}
return !file.exists();
}
}
告诉我你传递给文件的文件路径,如果你把文件写入SD卡,你将无法删除它因为 从 Android 4.4 开始,应用程序无法再写入 micro SD 卡。
这是我尝试了很多方法后使用的代码。
private static boolean delete(final Context context, final File file) {
final String where = MediaStore.MediaColumns.DATA + "=?";
final String[] selectionArgs = new String[] {
file.getAbsolutePath()
};
final ContentResolver contentResolver = context.getContentResolver();
final Uri filesUri = MediaStore.Files.getContentUri("external");
contentResolver.delete(filesUri, where, selectionArgs);
if (file.exists()) {
contentResolver.delete(filesUri, where, selectionArgs);
}
return !file.exists();
}