Следующий код, который состоит из загрузки файла с сервера и сохранения его в хранилище, работает нормально, когда устройство имеет внутреннее хранилище.
Но когда я попробовал это с устройством без внутренней памяти, только с внешней памятью, я получил следующее исключение.
java.io.filenotfoundexception открыть не удалось eacces (разрешение отклонено)
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.