【问题标题】:How to delete a file from storage using file's path?如何使用文件路径从存储中删除文件?
【发布时间】:2019-03-13 07:50:00
【问题描述】:

我正在开发一个应用程序,该应用程序列出了共享内部存储和可移动 SD 卡中的所有音频文件。 现在,如果用户想要删除特定文件,它将从共享内部存储或可移动 SD 卡中删除。

我面临的问题是 file.delete 不起作用,我已使用 mediastore 获取所有音频文件。

这些是我从媒体商店获得的音频文件路径。

这是来自内部共享存储。

/storage/emulated/0/Music/Guitar1.mp3

这是来自可移动微型 SD 卡。

/storage/BBF8-A8D3/Guitar1.mp3

得到这些路径后

  File deleteFile = new File(s.getFilepath());
  boolean delete = deleteFile.delete();

删除给false,因为删除文件没有被删除。

现在我已经尝试过了,

    File deleteFile = new File(s.getFilepath());
    if(deleteFile.exists()) {
    boolean catchdelete = deleteFile.delete();}

现在从路径创建文件后,如果条件失败,删除文件不存在。

那么为什么新创建的文件不存在(文件不是目录)是否需要文件输入流。

我的主要问题是通过应用程序从存储中删除文件。

这是我获取音频文件路径的方法

public ArrayList<String> getAudiosPath(Activity activity, Context context) {
            //  Uri uri;
            listOfAllAudios = new ArrayList<String>();
            Cursor cursor;

            final String[] columns = {MediaStore.Audio.Media.DATA, MediaStore.Audio.Media._ID};
            final String orderBy = MediaStore.Audio.Media._ID;
            //Stores all the audio from the gallery in Cursor
            cursor = getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns, null,
                    null, orderBy);
            //Total number of audios
            int count = cursor.getCount();

            //Create an array to store path to all the audios
            String[] arrPath = new String[count];

            for (int i = 0; i < count; i++) {
                cursor.moveToPosition(i);
                int dataColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);

                //Store the path of the audio
                arrPath[i] = cursor.getString(dataColumnIndex);
                Bitmap b = ((BitmapDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.headphone512, null)).getBitmap();

                bitmap.add(b);
                Log.i("PATH", arrPath[i]);
                listOfAllAudios.add(arrPath[i]);
            }

            //  count_paths=listOfAllAudios.size();

            return listOfAllAudios;
        }

现在我已经应用了 Apache Commons IO File Utils

 File deleteFile = new File(s.getFilepath());
     // boolean delete = deleteFile.delete();
      try {
          FileUtils.forceDelete(FileUtils.getFile(s.getFilepath()));
        } 
    catch (IOException e) {
                  e.printStackTrace();
        }

此 Apache Commons 文件实用程序确实删除了该文件,但问题是再次打开应用程序时,我看到文件大小为 0 KB 的文件路径。

在下载导航抽屉中

当在导航抽屉中时,我访问 TA-1032-> 音乐 -> 空(没有文件存在) (没有文件意味着文件被删除)

但在导航抽屉中,我访问音频-> 未知 -> 音乐 -> Guitar.mp3(文件存在,但文件大小为 0 且无法播放)

所以这是获取文件路径的一些方法。

【问题讨论】:

    标签: java filepath delete-file internal-storage


    【解决方案1】:

    尝试使用 Apache Commons 作为依赖项,并使用他们的文件 API 进行操作。

    FileUtils.forceDelete(FileUtils.getFile(s.getFilepath()));
    

    【讨论】:

    • 我尝试将 FileUtils 与 try catch 一起使用,它显示 File Not found 异常。虽然我收到的路径来自 file.getAbsolutepath();这给出了正确的存储路径。
    【解决方案2】:

    这件作品可能会奏效。

    Files.deleteIfExists(Paths.get("C:\\Users\\Mayank\\Desktop\\ 
            445.mp3"));
    

    【讨论】:

      【解决方案3】:

      代码片段会很有帮助

      File directory = new File("c:\\directoryname\\filename.txt");-- give your path where file is located.
      
              try {
      
                  FileUtils.forceDelete(directory);
      
                  System.out.println("force delete file in java");
      
              } 
              catch (IOException e) {
                  e.printStackTrace();
              }
      

      【讨论】:

        【解决方案4】:

        这对我有用,@dammina 发布的代码删除了该文件,但仍然可以从媒体商店访问,因此其他方法会处理它。

             File deleteFile = new File(s.getFilepaths());
        
           try {                                               
              FileUtils.forceDelete(FileUtils.getFile(s.getFilepaths()));                                             
               //   adapterRecycler.notifyDataChanged();                                              
                 adapterRecycler.notifyDataChanged(sectionHeaders);
                                                    }
                 catch (IOException e) {
                        e.printStackTrace();
                        }
        
        
         deleteFileFromMediaStore(getContentResolver(), deleteFile);
        

        从媒体存储中删除的方法,因为即使在删除文件之后,它仍然可以通过媒体存储访问。

        public static int deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
                    String canonicalPath;
                    try {
                        canonicalPath = file.getCanonicalPath();
                    } catch (IOException e) {
                        canonicalPath = file.getAbsolutePath();
                    }
                   // MediaStore.Files.FileColumns.DATA
                    final Uri uri = MediaStore.Files.getContentUri("external");
                    final int result = contentResolver.delete(uri,
                            MediaStore.Audio.Media.DATA + "=?", new String[]{canonicalPath});
                    if (result == 0) {
                        final String absolutePath = file.getAbsolutePath();
                        if (!absolutePath.equals(canonicalPath)) {
                            int deletedRow = contentResolver.delete(uri,
                                    MediaStore.Audio.Media.DATA + "=?", new String[]{absolutePath});
                            return deletedRow;
                        }
                    } else return result;
                    return result;
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-07-24
          • 1970-01-01
          • 2012-10-13
          • 1970-01-01
          • 2019-06-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多