I can save correctly the audio file in the directory i wanna, but when i try to open it on the phone, it says: " Couldn't play the track you request " .
to see the message click here
Basically i used this code and it worked well, but updating android studio and the project to android 11, the code just doesn't seems to work again..
Maybe the file is corrupted?
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);
}
}
question from:
https://stackoverflow.com/questions/65837639/cant-open-audio-file-saved-on-external-storage 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…