删除使用 uri 查找的文件不起作用
Deleting file that find using uri don't work
我想构建一个文件 Manager.I 使用 ContentResolver
获取所有文件的列表。例如,我通过这种方式获取所有音频文件:
private void getMusic(){
ContentResolver contentResolver=getContentResolver();
Uri songUri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor songCursor=contentResolver.query(songUri,null,null,null,null);
if(songCursor!=null && songCursor.moveToFirst()){
Log.e(TAG,"if :");
int songTitle=songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songPath=songCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
do{
String currentTitle=songCursor.getString(songTitle);
Uri currentPath=Uri.parse(songCursor.getString(songPath));
models.add(new model(currentTitle,currentPath));
adapter.notifyDataSetChanged();
}while (songCursor.moveToNext());
}
}
我写了这个删除文件的方法,但对 me.I 没有用,还从用户那里获得了 WRITE 和 READ 外部权限。
private void deleteItem(int position){
File fdelete = new File(models.get(position).getPath().toString());
if (fdelete.exists()) {
boolean flag=fdelete.delete();
if (flag) {
models.remove(models.get(position));
adapter.notifyDataSetChanged();
disableSelectionMode();
Toast.makeText(this,"File successfully deleted.",Toast.LENGTH_SHORT).show();
} else {
disableSelectionMode();
Toast.makeText(this,"File did not deleted!",Toast.LENGTH_SHORT).show();
}
}
}
这是我的模型:
public class model {
private int id;
private String title;
private Uri path;
public model(String title, Uri path) {
this.title = title;
this.path = path;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Uri getPath() {
return path;
}
public void setPath(Uri path) {
this.path = path;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
最后我发现 solution.I 应该使用 ContentResolver
删除文件。我是这样做的:
private void deleteItem(model item){
switch (type){
case AUDIOS:{
int b=getContentResolver().delete(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
MediaStore.MediaColumns.DATA + "='" + item.getPath().getPath() + "'", null
);
Log.e(TAG,"b: "+b);
break;
}
case IMAGES:{
int b=getContentResolver().delete(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.MediaColumns.DATA + "='" + item.getPath().getPath() + "'", null
);
Log.e(TAG,"b: "+b);
break;
}
case VIDEOS:{
int b=getContentResolver().delete(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
MediaStore.MediaColumns.DATA + "='" + item.getPath().getPath() + "'", null
);
Log.e(TAG,"b: "+b);
break;
}
case COMPRESSED:
case APPS:
case DOCUMENTS :{
int bex=getContentResolver().delete(
MediaStore.Files.getContentUri("external"),
MediaStore.MediaColumns.DATA + "='" + item.getPath().getPath() + "'", null
);
int bin=getContentResolver().delete(
MediaStore.Files.getContentUri("internal"),
MediaStore.MediaColumns.DATA + "='" + item.getPath().getPath() + "'", null
);
Log.e(TAG,"bex: "+bex);
Log.e(TAG,"bin: "+bin);
break;
}
}
}
我想构建一个文件 Manager.I 使用 ContentResolver
获取所有文件的列表。例如,我通过这种方式获取所有音频文件:
private void getMusic(){
ContentResolver contentResolver=getContentResolver();
Uri songUri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor songCursor=contentResolver.query(songUri,null,null,null,null);
if(songCursor!=null && songCursor.moveToFirst()){
Log.e(TAG,"if :");
int songTitle=songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songPath=songCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
do{
String currentTitle=songCursor.getString(songTitle);
Uri currentPath=Uri.parse(songCursor.getString(songPath));
models.add(new model(currentTitle,currentPath));
adapter.notifyDataSetChanged();
}while (songCursor.moveToNext());
}
}
我写了这个删除文件的方法,但对 me.I 没有用,还从用户那里获得了 WRITE 和 READ 外部权限。
private void deleteItem(int position){
File fdelete = new File(models.get(position).getPath().toString());
if (fdelete.exists()) {
boolean flag=fdelete.delete();
if (flag) {
models.remove(models.get(position));
adapter.notifyDataSetChanged();
disableSelectionMode();
Toast.makeText(this,"File successfully deleted.",Toast.LENGTH_SHORT).show();
} else {
disableSelectionMode();
Toast.makeText(this,"File did not deleted!",Toast.LENGTH_SHORT).show();
}
}
}
这是我的模型:
public class model {
private int id;
private String title;
private Uri path;
public model(String title, Uri path) {
this.title = title;
this.path = path;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Uri getPath() {
return path;
}
public void setPath(Uri path) {
this.path = path;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
最后我发现 solution.I 应该使用 ContentResolver
删除文件。我是这样做的:
private void deleteItem(model item){
switch (type){
case AUDIOS:{
int b=getContentResolver().delete(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
MediaStore.MediaColumns.DATA + "='" + item.getPath().getPath() + "'", null
);
Log.e(TAG,"b: "+b);
break;
}
case IMAGES:{
int b=getContentResolver().delete(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.MediaColumns.DATA + "='" + item.getPath().getPath() + "'", null
);
Log.e(TAG,"b: "+b);
break;
}
case VIDEOS:{
int b=getContentResolver().delete(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
MediaStore.MediaColumns.DATA + "='" + item.getPath().getPath() + "'", null
);
Log.e(TAG,"b: "+b);
break;
}
case COMPRESSED:
case APPS:
case DOCUMENTS :{
int bex=getContentResolver().delete(
MediaStore.Files.getContentUri("external"),
MediaStore.MediaColumns.DATA + "='" + item.getPath().getPath() + "'", null
);
int bin=getContentResolver().delete(
MediaStore.Files.getContentUri("internal"),
MediaStore.MediaColumns.DATA + "='" + item.getPath().getPath() + "'", null
);
Log.e(TAG,"bex: "+bex);
Log.e(TAG,"bin: "+bin);
break;
}
}
}