android环境.getExternalStorageDirectory().getPath()

android环境.getExternalStorageDirectory().getPath(),android,Android,我有以下代码: try { URL url = new URL (strURL); input = url.openStream(); byte[] buffer = new byte[1500]; OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png"); 我得到了这个错误: 不要硬编码/sd卡/使用environment

我有以下代码:

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");
我读过:


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

修改后的代码应该是这样的

OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+pos+".png");
您应该小心,因为并非所有设备都有/SD卡/路径


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

sd文件夹名称在某些手机中有所不同

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

control+shift+o->在eclipse中添加导入


问题是您调用了一个名为Environment.java的文件,因此Eclipse没有给我选择导入环境更改该文件。

OutputStream output=new FileOutputStream Environment.getExternalStorageDirectory.getAbsolutePath+file.separator+pos+.png;对不起,请问您为什么使用+pos+?
"/sdcard/" is replace with "Environment.getExternalStorageDirectory().getPath()" in your code