【问题标题】:How to delete music files with their path android 11 - java如何删除带有路径的音乐文件 android 11 - java
【发布时间】:2021-10-06 06:58:56
【问题描述】:

所以我有一个音乐播放器应用程序。我希望用户应该能够删除不是由我的应用程序创建的音乐文件。我有文件路径,我尝试使用 File.delete() 但它总是返回 false。如何使用它们的路径删除音乐文件。请有人帮忙。 我使用 File.getPath 获得的路径 - /storage/emulated/0/Samsung/Music/Over_the_horizo​​n.mp3

我的 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;
                        }```

【问题讨论】:

  • 你是如何获得路径的?你能把你要删除的文件的路径吗?
  • 我正在使用 File.getPath() 获取路径
  • 这个文件是目录吗?您可以编辑您的问题并粘贴您使用 File.getPath() 获得的内容吗?
  • 我编辑了请检查
  • 删除前是否检查过文件是否存在?

标签: java android android-studio android-mediaplayer delete-file


【解决方案1】:

/storage/emulated/0/Samsung/Music/Over_the_horizo​​n.mp3

这不是您的应用创建的文件。

在 Android 11 设备上,您无法使用经典文件系统工具读取/写入/删除此类文件,因为您的应用不是所有者。

File.canRead() 和 File.canWrite() 会告诉你。

要删除文件,请使用ACTION_OPEN_DOCUMENT 让用户先选择文件。

另一种可能性是获取该文件的媒体存储 uri,然后使用 MediaStore.createDeleteRequest() 进行删除。

【讨论】:

  • 你能告诉我更多关于 MediaStore.createDeleteRequest 的信息
【解决方案2】:

试试这个:

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 是String

【讨论】:

    【解决方案3】:

    你试过Context.deleteFile()吗?

    getApplicationContext().deleteFile(filename);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2013-07-01
    • 2017-09-19
    相关资源
    最近更新 更多