android - 如何将音频文件从原始文件夹保存到手机

标签 android audio mediastore raw-input saving-data

在下面,我有hiow的编码,可将音频设置为您的铃声。效果很好,但是我想知道如何更改此设置,使其很好地保存音频,以便可以与其他歌曲一起播放

public boolean save1(int type) {
    byte[] buffer = null;
InputStream fIn = getBaseContext().getResources().openRawResource(
        R.raw.song);
int size = 0;

try {
    size = fIn.available();
    buffer = new byte[size];
    fIn.read(buffer);
    fIn.close();
} catch (IOException e) {
    return false;
}

String path = Environment.getExternalStorageDirectory().getPath()
        + "/media/audio/";

String filename = "New song";

FileOutputStream save;
try {
    save = new FileOutputStream(path + filename);
    save.write(buffer);
    save.flush();
    save.close();
} catch (FileNotFoundException e) {
    return false;
} catch (IOException e) {
    return false;
}

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());
values.put(MediaStore.MediaColumns.TITLE, filename);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");

if (RingtoneManager.TYPE_RINGTONE == type) {
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
} else if (RingtoneManager.TYPE_NOTIFICATION == type) {
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
} else if (RingtoneManager.TYPE_ALARM == type) {
    values.put(MediaStore.Audio.Media.IS_ALARM, true);
}

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k
        .getAbsolutePath());
Uri newUri = MainActivity.this.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, type,
        newUri);

this.getContentResolver()
        .insert(MediaStore.Audio.Media.getContentUriForPath(k
                .getAbsolutePath()), values);
return true;
}

最佳答案

要将以下代码中的原始文件夹文件移动到sdcard或手机存储中,将起作用:

private void CopyRAWtoPhone(int id, String path) throws IOException {
    InputStream in = getResources().openRawResource(id);
    FileOutputStream out = new FileOutputStream(path);
    byte[] buff = new byte[1024];
    int read = 0;
    try { 
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        } 
    } finally { 
        in.close();
        out.close();
    } 
} 

其中第一个变量ID将是您的原始文件ID,字符串路径将是您的电话存储路径。

关于android - 如何将音频文件从原始文件夹保存到手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33841182/

相关文章:

android - 如何解决 MobiLink server has encountered an error and the synchro in SUP android

java - 无法让 android 服务工作

audio - 通过 VoIP 播放音频文件

java - 原生 Android 媒体播放器如何使数据持久化并反射(reflect)变化

android - 在 Android Q 上更改专辑封面

android - 长按不弹出上下文菜单

android - 如何使用 UiAutomator 访问覆盖窗口?

java - 我在尝试制作随机歌曲播放器时失败了

audio - SDL2 浮点音频不剪辑

java - Cursor getContentResolver().query MediaStore 未获取最新添加的音频文件