/mnt/sdcard和/sdcard有什么区别吗?

5
我正在尝试将位图保存到图片目录中。以下是代码:
            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' 的值是什么? - Name is Nilay
3个回答

3

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


1

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

编辑

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

WRITE_EXTERNAL_STORAGE

1
非常感谢您的回复,感谢您抽出时间。显然,为了使其正常工作,必须设置WRITE_EXTERNAL_STORAGE权限。我忽略了这一部分,因此即使调用mkdir()也会出现FileNotFoundException。 - user1694345

0

使用方法如下:

 String filePath = "/sdcard/yourfile.txt";

 FileOutputStream os = null;
 os = new FileOutputStream(filePath); 
 os.write(write it to file);
 os.close();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接