如何使用文件路径从存储中删除文件?

问题描述 投票:0回答:4

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

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

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

这来自内部共享存储。

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

这是可移动的micro 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文件utils会删除该文件,但问题是当再次打开应用程序时,我看到文件大小为0 KB的文件路径。

在下载导航抽屉中

Nav drawer

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

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

所以这是一些如何获取文件的路径。

java filepath delete-file internal-storage
4个回答
1
投票

尝试使用Apache Commons作为依赖项并使用其文件API进行操作。

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

1
投票

这件作品可能有用。

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

1
投票

代码片段将有所帮助

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();
        }

1
投票

这对我有用,@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;
        }
© www.soinside.com 2019 - 2024. All rights reserved.