添加图片文件到存储卡的源码如下:
void createExternalStoragePublicPicture() {
// Create a path where we will place our picture in the user's
// public pictures directory. Note that you should be careful about
// what you place here, since the user often manages these files. For
// pictures and other media owned by the application, consider
// Context.getExternalMediaDir().
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
try {
// Make sure the Pictures directory exists.
path.mkdirs();
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
OutputStream os = new FileOutputStream(file);
InputStream is = getResources().openRawResource(R.drawable.balloons);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
从存取卡中读取图形文件的代码如下:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
在其他手机都写和读正常。独独破小米3。
然后1.权限检查。2.bug跟踪还是提示FileNoFound和EISDIR。
3.上网搜,说更新驱动。更新了也没屁用。
4.关闭USB调试。
5.root工具查看权限,还是不行。但奇怪的是我的写卡权限居然在小米中查不到。看了ES浏览器等也没有提示。估计小米屏蔽了。晕死。
5.关闭USB调试后作为MTP设备连接电脑。查看到居然有两个/storage,其中一个下面有/storage/emulated/legacy/MIUI。一个下面有/storage/emulated/0。果断删除前一个。运行程序。正常。
2015.4.27
删除了好多下面的音乐文件。哭。
后来采用新的方法。直接用绝对路径来保存文件。比如:file:///sdcard/1.mp3。