所以我想要的是使用URI删除文件
请勿标记为重复
我尝试了很多答案,但这里没有解决的问题
How to delete file that is created using Uri?
How to delete file that is created using Uri?
我编写了从图库中获取图像的代码,然后将其复制到名为“ .blackhat”的目录中,并删除了原始图像(一种“移动文件功能”)。即使它不是生成日志,所以我可以检查错误。 此代码适用于复制文件,但不能在复制后删除。...
if(requestCode == 2 && data.getData() !=null){
if(cd == null){
path.add(data.getData());
Random rn = new Random();
if(copyFileFromUri(this,data.getData(),String.valueOf(rn.nextInt(500)))){
File fdelete = new File(data.getData().getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
Log.d("delete","deleted");
} else {
Log.d("delete","not deleted");
}
}
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
}else{
for(int i=0;i<data.getClipData().getItemCount();i++){
path.add(data.getClipData().getItemAt(i).getUri());
Log.d("RjList",path.get(i).toString());
Random rn=new Random();
if(copyFileFromUri(this,data.getClipData().getItemAt(i).getUri(),String.valueOf(rn.nextInt(500)))){
File fdelete = new File(data.getClipData().getItemAt(i).getUri().getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
Log.d("delete","deleted");
} else {
Log.d("delete","not deleted");
}
}
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
}
}
}
我在哪里做错了???? 预先感谢...
答案 0 :(得分:1)
public static boolean delete(final Context context, final File file) {
final String pathone = MediaStore.MediaColumns.DATA + "=?";
final String[] selectedArgs = new String[] {
file.getAbsolutePath()
};
final ContentResolver contentResolver = context.getContentResolver();
final Uri fileUri = MediaStore.Files.getContentUri("external");
contentResolver.delete(fileUri, pathone, selectedArgs );
if (file.exists()) {
contentResolver.delete(fileUri, pathone, selectedArgs );
}
return !file.exists();
}
答案 1 :(得分:0)
File fdelete = new File(data.getData().getPath(),"here you should pass the file name");
if (fdelete.exists()) {
if (fdelete.delete()) {
Log.d("delete","deleted");
} else {
Log.d("delete","not deleted");
}
}