问题描述
我可以将音频文件正确保存在我想要的目录中,但是当我尝试在手机上打开它时,它说:“无法播放您请求的曲目”。 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 (将#修改为@)