【问题标题】:is there a difference between /mnt/sdcard and /sdcard?/mnt/sdcard 和 /sdcard 有区别吗?
【发布时间】:2012-09-24 12:16:22
【问题描述】:

我正在尝试将位图保存到图片目录中。这是代码

            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

            File file = new File(path, "test1.PNG");
            try { 
                   path.mkdirs();
                   OutputStream out = new FileOutputStream(file);
                   mBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                   out.flush();
                   out.close();

            } catch (Exception e) {
                   e.printStackTrace();
                   Log.w("ExternalStorage", "Error writing " + file, e);
            }

但是执行卡在OutputStream out = new FileOutputStream(file); 我使用了调试器,完整路径返回mnt/sdcard/Pictures/test1.PNGmnt/ 是我无法通过OutputStream out = new FileOutputStream(file); 的罪魁祸首吗?因为在我的文件目录中只能看到sdcard/

谢谢!

【问题讨论】:

  • 'path'的值是什么..??

标签: android android-image android-file


【解决方案1】:

/sdcard 是指向/mnt/sdcard 的软链接... 并且/sdcard 在文件系统中是只读的,所以最好使用/mnt/sdcard/..

【讨论】:

    【解决方案2】:

    您可以使用此Environment.getExternalStorageDirectory() 作为 mnt/sdcard 或 sdcard/ 获取和访问 sdcard 目录,它是一个设备相关目录,操作系统如何访问和使用外部目录,无需担心不同的设备和不同的目录通过这种方式返回。

    编辑

    访问外部存储需要权限并在androidmanifest.xml文件中定义为用户权限

    WRITE_EXTERNAL_STORAGE
    

    【讨论】:

    • 非常感谢您的回复,感谢您抽出宝贵的时间。显然要让它工作,必须为 WRITE_EXTERNAL_STORAGE 设置权限,我忽略了这部分,因此即使在调用 mkdir() 之后也会出现 FileNotFoundException。
    【解决方案3】:

    这样使用

     String filePath = "/sdcard/yourfile.txt";
    
     FileOutputStream os = null;
     os = new FileOutputStream(filePath); 
     os.write(write it to file);
     os.close();
    

    【讨论】:

      猜你喜欢
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多