写入android 11中的音乐目录
我的任务是让一个非常旧的 Android 应用程序面向最新的 Android 版本,其中包括 Android 11 中引入的范围存储更改。
该应用程序“具有”将音频播放列表导出到外部共享存储的工作功能。在已经提出问题之后,现在很明显,我唯一的选择可能是在共享音乐文件夹中写入,因此我更改了以下内容:
File ext = Environment.getExternalStorageDirectory();
File newDir = new File(ext + "/" + "coachyyplayerplaylists");
更改
File ext = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
File newDir = new File(ext + "/" + "coachyyplayerplaylists");
后,该文件夹确实在音乐文件夹中创建,但在尝试写入时没有任何反应除了抛出 IOException 之外。
这是完整的代码(不要介意,我知道它真的很糟糕):
try {
File ext = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
File newDir = new File(ext + "/" + "coachyyplayerplaylists");
if (!newDir.exists()) {
newDir.mkdir();
}
String name = currentPlaylist.filePath.substring(currentPlaylist.filePath.lastIndexOf("/") + 1);
boolean didWriteAll = true;
String s = "";
for (Song song : currentPlaylist.getSongs(this)) {
if (song.title != null && song.title.length() > 0 && song.artist != null && song.artist.length() > 0) {
s += (song.title + "\n" + song.artist + "\n" + song.duration + "\n");
} else {
didWriteAll = false;
}
}
if (s.length() == 0) {
Toast.makeText(this, "Fehler beim Export: Nur Songs mit Titel und Artist können exportiert werden.",
Toast.LENGTH_SHORT).show();
} else {
if (!didWriteAll) {
Toast.makeText(this,
"Nicht alle Songs wurden exportiert. Nur Songs mit Titel und Artist können exportiert werden.",
Toast.LENGTH_LONG).show();
}
FileWriter writer = new FileWriter(ext + "/" + "coachyyplayerplaylists/" + name + ".coachyyplaylist");
writer.write(s);
writer.close();
String[] toBeScannedStr = new String[1];
toBeScannedStr[0] = ext + "/" + "coachyyplayerplaylists/" + name + ".coachyyplaylist";
MediaScannerConnection.scanFile(this, toBeScannedStr, null, null);
showMessage("Die Playlist wurde unter " + ext + "/" + "coachyyplayerplaylists/" + name +
".coachyyplaylist gespeichert");
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Ein Fehler ist aufgetreten!", Toast.LENGTH_SHORT).show();
}
}
所以我想知道的第一件事是这种方法是否可行,如果是的话上面的代码有什么问题?
抛出的异常如下:
java.io.IOException: write failed: EBUSY (Device or resource busy)
My task is to make a really old android app target the latest android version which includes the scoped storage changes introduced in android 11.
The app "had" the working feature to export audio playlists into the external shared storage. After already asking a question it is now clear that my only option probably is to write in the shared music folder so I changed the following:
File ext = Environment.getExternalStorageDirectory();
File newDir = new File(ext + "/" + "coachyyplayerplaylists");
to
File ext = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
File newDir = new File(ext + "/" + "coachyyplayerplaylists");
After changing that the folder does get created in the music folder but when trying to write to it nothing happens other than an IOException being thrown.
Here is the full code(Don't mind it I know its really bad):
try {
File ext = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
File newDir = new File(ext + "/" + "coachyyplayerplaylists");
if (!newDir.exists()) {
newDir.mkdir();
}
String name = currentPlaylist.filePath.substring(currentPlaylist.filePath.lastIndexOf("/") + 1);
boolean didWriteAll = true;
String s = "";
for (Song song : currentPlaylist.getSongs(this)) {
if (song.title != null && song.title.length() > 0 && song.artist != null && song.artist.length() > 0) {
s += (song.title + "\n" + song.artist + "\n" + song.duration + "\n");
} else {
didWriteAll = false;
}
}
if (s.length() == 0) {
Toast.makeText(this, "Fehler beim Export: Nur Songs mit Titel und Artist können exportiert werden.",
Toast.LENGTH_SHORT).show();
} else {
if (!didWriteAll) {
Toast.makeText(this,
"Nicht alle Songs wurden exportiert. Nur Songs mit Titel und Artist können exportiert werden.",
Toast.LENGTH_LONG).show();
}
FileWriter writer = new FileWriter(ext + "/" + "coachyyplayerplaylists/" + name + ".coachyyplaylist");
writer.write(s);
writer.close();
String[] toBeScannedStr = new String[1];
toBeScannedStr[0] = ext + "/" + "coachyyplayerplaylists/" + name + ".coachyyplaylist";
MediaScannerConnection.scanFile(this, toBeScannedStr, null, null);
showMessage("Die Playlist wurde unter " + ext + "/" + "coachyyplayerplaylists/" + name +
".coachyyplaylist gespeichert");
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Ein Fehler ist aufgetreten!", Toast.LENGTH_SHORT).show();
}
}
So the first thing that I wonder is if this approach is even doable and if yes what is wrong with the code above?
The exception that is being thrown is the following:
java.io.IOException: write failed: EBUSY (Device or resource busy)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论