skip to Main Content

So I have a music player app. In which I want the user should be able to delete the music files which are not created by my app. I have the file path and I tried Using File.delete() but it always returns false. How can I delete the music files using their path. Please can someone help.
Path I am getting using File.getPath – /storage/emulated/0/Samsung/Music/Over_the_horizon.mp3

My AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.naman.musicplayer">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MusicPlayer">
        <activity
            android:name=".PlaySong"
            android:exported="true" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

**Code I am using to delete the music**

     ``` @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
              File file = files.get(i);
    //files = new ArrayList<File>();
               boolean deleted = file.delete();
               return deleted;
                        }```

2

Answers


  1. /storage/emulated/0/Samsung/Music/Over_the_horizon.mp3

    That is a file not created by your app.

    On an Android 11 device you cannot read/write/delete such a file with classic file system tools as your app is not the owner.

    File.canRead() and File.canWrite() would tell you that.

    To delete the file use ACTION_OPEN_DOCUMENT to let the user pick the file first.

    Another possibility is to get the mediastore uri for that file and then use MediaStore.createDeleteRequest() for the delete.

    Login or Signup to reply.
  2. try this :

    File file = new File(path);
    
    if (!file.exists()) return;
    
    if (file.isFile()) {
    file.delete();
    return;
    }
    
    File[] fileArr = file.listFiles();
    
    if (fileArr != null) {
    for (File subFile : fileArr) {
    if (subFile.isDirectory()) {
    deleteFile(subFile.getAbsolutePath());
    }
    
    if (subFile.isFile()) {
    subFile.delete();
    }
    }
    }
    
    file.delete();
    

    path is a String

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search