所以我有一个音乐播放器应用程序。在其中,我希望用户应该能够删除音乐文件,而不是我的应用程序创建。我有文件路径,我尝试使用File.delete(),但是它总是返回false。如何使用它们的路径删除音乐文件。有人能帮忙吗。使用File.getPath -File.getPath获得的路径
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;
}```发布于 2021-10-09 14:04:19
试试这个:
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
发布于 2021-10-06 10:47:26
/storage/emulated/0/Samsung/Music/Over_the_horizon.mp3
这是一个没有由你的应用程序创建的文件。
在Android 11设备上,您不能用经典的文件系统工具来读取/写/删除这样的文件,因为您的应用程序不是所有者。
File.canRead()和File.canWrite()会告诉您这一点。
要删除该文件,请使用ACTION_OPEN_DOCUMENT先让用户选择该文件。
另一种可能是获取该文件的纵隔uri,然后使用MediaStore.createDeleteRequest()进行删除。
https://stackoverflow.com/questions/69461165
复制相似问题