Android 小米手机 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)读取文件失败。

添加图片文件到存储卡的源码如下:

   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。  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值