0
我試圖將嵌入到我的應用程序的mp3資源保存到android設備。所以我可以在默認的Android媒體播放器中播放它。安卓原生mp3資源原生安卓媒體存儲
我可以得到我的資源inputstream沒有問題。但我無法將它保存到設備的默認Java方式。
而且這兩種方案似乎並沒有爲我工作:
How can I download a song and add it the user's music library?
我試圖將嵌入到我的應用程序的mp3資源保存到android設備。所以我可以在默認的Android媒體播放器中播放它。安卓原生mp3資源原生安卓媒體存儲
我可以得到我的資源inputstream沒有問題。但我無法將它保存到設備的默認Java方式。
而且這兩種方案似乎並沒有爲我工作:
How can I download a song and add it the user's music library?
首先確保你的應用程序有適當的權限,包括android.permission.WRITE_EXTERNAL_STORAGE
然後你就可以複製從您的資源到Android設備的文件。下面是說明目的只有一個示例代碼,更改需要:
private void copyMp3() throws IOException{
// Open your mp3 file as the input stream
InputStream myInput = getAssets().open("your_file.mp3");
// Path to the output file on the device
String outFileName = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC),"your_file.mp3");
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams => Better to have it in *final* block
myOutput.flush();
myOutput.close();
myInput.close();
}
媒體掃描儀應該由自己挑選的文件(除非該文件夾中的文件.nomedia),但如果你想加快進程速度,您可以使用您在問題中提到的鏈接。