user4914918
user4914918

Reputation: 91

How to save audio file from raw folder to phone

Below I have the coding for hiow to set audio as your ringtone. This works fine however I wanna know how could I alter this such that it well save just the audio so that it can be played with my other songs

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;
}

Upvotes: 0

Views: 1653

Answers (1)

Anjali
Anjali

Reputation: 2535

To move raw folder file to sdcard or phone storage below code will work:

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();
    } 
} 

where first Variable id will be your raw file id and String path will be your phone storage path.

Upvotes: 1

Related Questions