Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

java - Can't open Audio File saved on External Storage

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...