如何解决无法打开保存在外部存储设备上的音频文件
我可以将音频文件正确保存在我想要的目录中,但是当我尝试在手机上打开它时,它说:“无法播放您请求的曲目”。 to see the message click here
基本上我使用了这段代码并且它运行良好,但是将 android studio 和项目更新到 android 11,代码似乎无法再次运行..
也许文件损坏了?
final String path = Environment.getExternalStorageDirectory()+ "/audio_files/";
public void saveas(int ressound,String name){
byte[] buffer;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size;
try {
size = fIn.available();
buffer = new byte[size];
fIn.close();
} catch (IOException e) {
// Todo Auto-generated catch block
return;
}
String filename= "(Character_name) " + name +".mp3";
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// Todo Auto-generated catch block
return;
} catch (IOException e) {
// Todo Auto-generated catch block
return;
}
sendbroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse("file://"+path+filename)));
File k = new File(path,filename);
ContentValues values = new ContentValues();
values.put( MediaStore.MediaColumns.DATA,k.getAbsolutePath());
//Insert it into the database
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.getContentResolver().insert(Objects.requireNonNull(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath())),values);
}
else{
this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()),values);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。