android Environment.getExternalStorageDirectory()。getPath()

时间:2013-10-18 05:50:17

标签: android

我有这段代码:

try {
            URL url = new URL (strURL);
            input = url.openStream();
            byte[] buffer = new byte[1500];
            OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");

我收到此错误: 不要硬编码/ SD卡/使用environment.getexternalstoragedirectory()。getpath()代替 在

 OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");

我已经读过:Android 4.2 - Environment.getExternalStorageDirectory().getPath() behaviour

所以问题是当我替换它时最终代码是什么?

2 个答案:

答案 0 :(得分:6)

某些手机中的sd文件夹名称不同

在三星手机中,它被命名为external_sd,您的代码将失败。

control + shift + o - >在eclipse中添加导入,see this link

"/sdcard/" is replace with "Environment.getExternalStorageDirectory().getPath()" in your code

问题是你调用了一个名为Environment.java的文件本身,所以Eclipse没有让我选择导入环境改变那个。

答案 1 :(得分:3)

您的修改后的代码应该是

OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+pos+".png");

您应该小心,因为并非所有设备都有/sdcard/路径。

多年来的设备已经发生变化,但这可能并不总是包含指向正确外部存储目录的链接。

相关问题