【发布时间】:2013-07-06 14:49:27
【问题描述】:
以下代码包括从服务器下载文件并将其保存在存储中,当设备具有内部存储时,它可以正常工作。
但是,当我尝试使用没有内部存储的设备时,仅使用外部存储时出现以下异常。
java.io.filenotfoundexception open failed eacces (permission denied)
public void downloadFile(String dlUrl, String dlName) {
int count;
HttpURLConnection con = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL( dlUrl );
con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.connect();
is = url.openStream();
String dir = Environment.getExternalStorageDirectory() + Util.DL_DIRECTORY;
File file = new File( dir );
if( !file.exists() ){
file.mkdir();
}
Util.LOG_W(TAG, "Downloading: " + dlName + " ...");
fos = new FileOutputStream(file + "/" + dlName);
byte data[] = new byte[1024];
while( (count = is.read(data)) != -1 ){
fos.write(data, 0, count);
}
Util.LOG_D(TAG, dlName + " Download Complete!");
} catch (Exception e) {
Util.LOG_E(TAG, "DOWNLOAD ERROR = " + e.toString() );
bServiceDownloading = false;
}
finally{
try {
if( is != null)
is.close();
if( fos != null)
fos.close();
if( con != null)
con.disconnect();
} catch (Exception e) {
Util.LOG_E(TAG, "CLOSE ERROR = " + e.toString() );
}
}
}
在清单文件中,我有以下内容:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
任何建议可能是什么原因? 顺便说一句,Environment.getExternalStorageDirectory() 返回 /mnt/sdcard/ 并且 file.mkdir() 返回 false。
【问题讨论】:
-
试试
File file = new File( dir + "/" + dlName ); -
不幸的是,结果是一样的。顺便说一句,file.mkdir() 返回 false,这就是我认为的问题。 @PankajKumar
-
如果您在模拟器上调试,请确保您创建的 sd 卡支持.. 或者如果真实设备确保 sd 卡存在(设备未连接 USB)
-
实机调试。存在 SD 卡。如何检查设备是否未连接 USB?这包括 USB 鼠标吗?
-
@hB0 我没有以理想的方式解决它。但我已经发布了关于我如何处理这个问题的答案。希望对您有所帮助。
标签: android android-permissions android-file