1.文件位于项目的assets目录下
1)assets目录下的xml文件
- InputStream inputStream = this.getAssets().open("config.xml");
- InputStream inputStream = this.getClass().getResourceAsStream("/assets/config.xml");
2)assets目录下的音乐文件
<span style="white-space:pre"> </span>AssetFileDescriptor fileDescriptor = getAssets().openFd("kalimba.mp3");
mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor());
2.res目录下的文件夹raw目录下的文件
mediaPlayer = MediaPlayer.create(this, R.raw.wusuowei);
3.手机中的内存
// /storage/emulated/0(手机打开自带的文件管理程序后看到的目录,是手机本身内置SD卡的16G、64G的内存)
System.out.println(Environment.getExternalStorageDirectory().getAbsolutePath()+"----getExternalStorageDirectory----");
System.out.println(Environment.getExternalStorageDirectory().getPath()+"-----getExternalStorageDirectory---");
// /data DDMS中根目录的data目录
System.out.println(Environment.getDataDirectory().getPath()+"-----getDataDirectory---");
System.out.println(Environment.getDataDirectory().getAbsolutePath()+"-----getDataDirectory---");
// /cache----getDownloadCacheDirectory---
System.out.println(Environment.getDownloadCacheDirectory().getPath()+"----getDownloadCacheDirectory---");
System.out.println(Environment.getDownloadCacheDirectory().getAbsolutePath()+"-----getDownloadCacheDirectory---");
// /system-----getRootDirectory---
System.out.println(Environment.getRootDirectory().getPath()+"-----getRootDirectory---");
System.out.println(Environment.getRootDirectory().getAbsolutePath()+"-----getRootDirectory---");
<span style="white-space:pre"> </span>try {
//new MediaPlayer();的方式,拿到assets目录下的文件。
mediaPlayer = new MediaPlayer();
AssetFileDescriptor fileDescriptor = getAssets().openFd("kalimba.mp3");
mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor());
mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(), fileDescriptor.getLength());
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/wusuowei.wav");
mediaPlayer.prepare();
mediaPlayer.start();
/MediaPlayer.create(this, R.raw.wusuowei);的方式,拿到raw目录下的文件。
mediaPlayer = MediaPlayer.create(this, R.raw.wusuowei);
mediaPlayer.start();
} catch (Exception e1) {
e1.printStackTrace();
}