如何使用我的应用程序删除图像文件?
File file = new File("path"); //PATH is: /storage/sdcard0/DCIM/Camera/IMG_20160913_165933.jpg
file.delete(); // try this one but not delete file....
boolean isDelete = file.delete(); //this also not delete...
context.deleteFile(file); // thisn one also not working in my example....
答案 0 :(得分:1)
试试这个工作代码
File target = new File(path);
Log.d(" target_path", "" + path);
if (target.exists() && target.isFile() && target.canWrite()) {
target.delete();
Log.d("d_file", "" + target.getName());
}
添加AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:1)
如果您尝试删除图片或类似图片,则可能会遇到问题。可能会返回结果为true,但文件仍然存在。我浪费了太多时间,最适合我的是:
private void deleteImage(File file) {
// Set up the projection (we only need the ID)
String[] projection = {MediaStore.Images.Media._ID};
// Match on the file path
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[]{file.getAbsolutePath()};
// Query for the ID of the media matching the file path
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst()) {
// We found the ID. Deleting the item via the content provider will also remove the file
long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
contentResolver.delete(deleteUri, null, null);
} else {
// File not found in media store DB
}
c.close();
}
答案 2 :(得分:0)
检查file.isExist()然后删除并检查AndroidManifest.xml文件中的权限。
答案 3 :(得分:0)
试试这个..
权限也添加到mainfiest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
用于marashmallow运行时权限添加
private static final int REQUEST_RUNTIME_PERMISSION = 123;
if (CheckPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// you have permission go ahead
File file = new File("path");
if(file.exists())
{
file.delete();
}
} else {
// you do not have permission go request runtime permissions
RequestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
}
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case REQUEST_RUNTIME_PERMISSION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// you do not have permission show toast.
}
return;
}
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
public boolean CheckPermission(Activity context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
答案 4 :(得分:0)
//PATH is: /storage/sdcard0/DCIM/Camera/IMG_20160913_165933.jpg
这似乎是removable storage。在Android 4.4+设备上,您无法在可移动存储上的任意位置读取,写入或删除文件。