我列出了sdcard /内部存储和播放中的音频和视频文件(参见下面的代码),但它不适用于大型音频/视频文件。它显示 OutOfMemoryError ,如何删除此错误?
public class GetPath extends Activity {
String imagePath;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.get_path);
}
public void getAudio(View v){
Intent i = new Intent(this, AudioList.class);
startActivityForResult(i, 11);
}
public void getVideo(View v) throws IOException {
Intent intent = new Intent(this, VideoList.class);
startActivityForResult(intent, 111);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
TextView imagePathText = (TextView)findViewById(R.id.image_path);
TextView audioPathText = (TextView)findViewById(R.id.audio_path);
TextView videoPathText = (TextView)findViewById(R.id.video_path);
byte [] audioByte = new byte[0];
byte[] videoByte = new byte[0];
if(requestCode == 11){
if(resultCode == 300){
Toast.makeText(getApplicationContext(),""+data.getExtras().getString("audioPath"), Toast.LENGTH_LONG).show();
audioPathText.setText(data.getExtras().getString("audioPath"));
imagePath = data.getExtras().getString("audioPath");
playMp3(audioByte);
MediaPlayer m = new MediaPlayer();
try {
m.setDataSource(imagePath);
m.prepare();
} catch (IOException e) {
e.printStackTrace();
}
m.start();
}
}else if(requestCode == 111){
if(resultCode == 301){
Toast.makeText(getApplicationContext(),""+data.getExtras().getString("videoPath"),Toast.LENGTH_LONG).show();
videoPathText.setText(data.getExtras().getString("videoPath"));
imagePath = data.getExtras().getString("videoPath");
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.play_video);
dialog.setTitle("video");
final VideoView videoplay = (VideoView)dialog.findViewById(R.id.videoPlayer);
final Button play = (Button)dialog.findViewById(R.id.play);
videoplay.setVideoPath(imagePath);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fff){
videoplay.start();
fff =false;
}else{
videoplay.pause();
fff =true;
}
}
});
dialog.show();
}
}
}
下面的代码将音频/视频文件转换为字节数组(无法转换大文件:我使用了70个MD音频文件和35MB视频文件)
public byte[] convert(String path) throws IOException {
FileInputStream fis = new FileInputStream(path);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[10024];
for (int readNum; (readNum = fis.read(b)) != -1;) {
bos.write(b, 0, readNum);
}
byte[] bytes = bos.toByteArray();
Toast.makeText(getApplicationContext(), ""+bytes.length, Toast.LENGTH_LONG).show();
return bytes;
}
下面的代码将字节数组转换为音频文件()
private void playMp3(byte[] mp3SoundByteArray) {
try {
File tempMp3 = File.createTempFile("kurchina", "mp3", getCacheDir());
tempMp3.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempMp3);
fos.write(mp3SoundByteArray);
fos.close();
MediaPlayer mediaPlayer = new MediaPlayer();
FileInputStream fis = new FileInputStream(tempMp3);
mediaPlayer.setDataSource(fis.getFD());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException ex) {
String s = ex.toString();
ex.printStackTrace();
}
}