在Android中无法删除文件 [英] Can't delete file in Android

查看:844
本文介绍了在Android中无法删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望(没有SD卡)中的音乐文件夹存储在我的手机上删除音乐文件。我有文件的路径和 File.exists()说真的。接下来, File.delete()也说真的,但是文件留在自己的位置。但!之后删除我再也无法播放该文件,修改名称,也不能复制它。但我可以手动删除它。

我已经设置 android.permission.WRITE_EXTERNAL_STORAG​​E 。 4.4.4操作系统GPE

哪里是我的错?任何建议,谢谢。

 文件fil​​e =新的文件(路径);
    如果(file.exists()){
        file.setWritable(真,假);
        返回file.delete();
    }


解决方案

所以,我似乎工作最终的解决方案是:

 公共布尔deleteTrack(字符串键){
    MusicTrack跟踪= getAlbum(0).getTrackByKey(密钥);
    如果(轨道== NULL)返回false;    档案文件=新的文件(track.getData()的路径。);
    如果(file.exists()){
        file.setWritable(真,假);
        。字符串,其中= MediaStore.Audio.Media.DATA += \\+ track.getData()路径+\\;
        如果(context.getContentResolver()。删除(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,其中,空)== 1){
            如果(file.exists()){
                布尔D = file.delete();
                context.sendBroadcast(新意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(文件)));
                返回D组;
            }
            返回true;
        }
    }
    返回false;
}

I wish delete music file stored on my phone (has no SD card) in Music folder. I have file path and File.exists() said true. Next, File.delete() also said true, but file stay in its place. But! After "deletion" I can no longer play this file, edit name and also can't copy it. But I can delete it manually.

I've set android.permission.WRITE_EXTERNAL_STORAGE. OS 4.4.4 GPE

Where is my mistake? Any suggestions, thanks.

    File file = new File(Path);
    if (file.exists()){
        file.setWritable(true, false);
        return file.delete();
    }

解决方案

So, my final solution that seems to work is:

public Boolean deleteTrack(String key){
    MusicTrack track = getAlbum(0).getTrackByKey(key);
    if (track == null) return false;

    File file = new File(track.getData().Path);
    if (file.exists()){
        file.setWritable(true, false);
        String where = MediaStore.Audio.Media.DATA +"=\""+ track.getData().Path +"\"";
        if (context.getContentResolver().delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, where, null) == 1){
            if (file.exists()){
                Boolean d = file.delete();
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
                return d;
            }
            return true;
        }
    }
    return false;
}

这篇关于在Android中无法删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆