无法在Android中删除文件

时间:2019-05-25 13:28:32

标签: java android file video internal-storage

问题:

我想从android设备的内部存储中删除视频文件。

  

下面的代码在扩展BaseAdapter的类中,并且“ file.delete()”方法返回false

    File fdelete = new File(videolist.get(position).getVideopath());
                        Log.d(TAG,"Path to delete : "+videolist.get(position).getVideopath());
                        if (fdelete.exists()) {
                            Log.d(TAG,"DELETE EXIST");
                            if (fdelete.delete()) {
                                Log.d(TAG,"DELETED");
                            } else {
                                Log.d(TAG,"NOT DELETED");
                            }
                        }
  

“ videolist.get(position).getVideopath()”返回以下内容:“ /storage/emulated/0/Download/jellyfish-3-mbps-hd-h264xgdhdudtudutdutdjtditditdtidjtditdjtdtkd.mkv”

1 个答案:

答案 0 :(得分:0)

尝试一下

    private void deleteMedia(final String advName) {
    File path = Environment.getExternalStorageDirectory();
    File directory = new File(path.getAbsolutePath() + "/your directory name");

    File[] files = directory.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.contains(advName);
        }
    });
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            file.delete();
        }
    }
}