Reputation: 25
I found this source code on the net and have modified it a little. But I get an error saying: java.io.FileNotFoundException /data/datafile.zip. What should I do to get it running? Do I have to create the file first?
Thanks, Sigurd
private Thread checkUpdate = new Thread() {
public void run() {
try {
long startTime = System.currentTimeMillis();
Log.d("Zip Download", "Start download");
File file = new File(Environment.getDataDirectory(), "datafil.zip");
Log.d("Zip Download", file.getAbsolutePath());
URL updateURL = new URL("http://dummy.no/bilder/bilder/XML_Item_Expo_01.zip");
URLConnection conn = updateURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("Zip Download", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
} catch (Exception e) {
Log.d("Zip Download", "Error: " + e);
}
}
};
Upvotes: 0
Views: 274
Reputation: 63303
Environment.getDataDirectory()
does not return a path where you can place files. You should use one of these methods instead:
Environment.getExternalStorageDirectory()
gives you a path to external storage (SD card).getFilesDir()
from an Activity or other Context. Gives a path to app's internal file storageYou can also call openFileOutput()
with a string file name (no path, just the file), which will open the FileOutputStream and create the file all in one shot for your use.
Hope that Helps!
Upvotes: 1
Reputation: 10651
Seems like permission error. You maybe writing to the wrong place. Check that answer at link below,
Data directory has no read/write permission in Android
Upvotes: 1