Search code examples
javafilepathdelete-fileinternal-storage

How to delete a file from storage using file's path?


I am working on an app that list all the audio files from shared internal storage and removable SD card. Now if the user wants to delete a particular file , it will be deleted from shared internal storage or removable SD card.

The issue I am facing is file.delete does not work , I have used mediastore to get all audio files.

These were the audio file paths i got from media store.

This is from internal shared storage.

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

This is from removable micro SD card.

/storage/BBF8-A8D3/Guitar1.mp3

After getting these paths

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

The delete gives false as delete file is not deleted.

Now I have tried this,

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

Now after creating file from path , if condition fails as delete File does not exist.

So why newly created file does not exist(file is not a directory) does it require file input stream.

My main issue is to delete a file from storage through the app.

This is my method for retreiving audio file paths

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

Now i have applied 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();
        }

This Apache Commons file utils does delete the file but issue is when opening the app again i am seeing the file path with file size 0 KB.

In Download Nav drawer

Nav drawer

when in Nav drawer i access TA-1032-> Music -> Empty(No File present) (there is no file which means file gets deleted)

But in Nav drawer i access Audio-> Unknown -> Music -> Guitar.mp3 (file present but file size is 0 and cant be played)

so this is some how getting the path of file.


Solution

  • Try using Apache Commons as a dependency and use their file API for the operation.

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