从raw中读取MP3文件
2018年02月10日
⁄ 综合
⁄ 共 1318字 ⁄ 字号
小 中 大
-
读取raw资源文件中的mp3文件,然后通过音乐播放器播放:
-
-
-
-
-
-
-
-
-
-
private void writeMP3ToSDcard(String fileName, Context context) {
-
byte[] buffer = new byte[1024 * 8];
-
int read;
-
BufferedInputStream bin = new BufferedInputStream(context.getResources().openRawResource(R.raw.ring));
-
try {
-
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fileName));
-
while ((read = bin.read(buffer)) > -1) {
-
bout.write(buffer, 0, read);
-
}
-
bout.flush();
-
bout.close();
-
bin.close();
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
-
-
Intent intent = new Intent();
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
intent.setAction(android.content.Intent.ACTION_VIEW);
-
intent.setDataAndType(Uri.fromFile(newFile("XXXXmp3的文件全路径")),"audio/*");
-
startActivity(intent);