So I want to download a file from a permanent URL and then save it locally so I can access it later to write into an SQLite database. I have an AsynchTask
class that handles this functionality but I'm getting a FileNotFoundException
error all the time (see Logcat).
Download CSV file class
private class CSVFileDownloader extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1),
new UsernamePasswordCredentials("username", "password"));
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.cardigan.cc/app/locations.csv");
//GET FILE FROM URL AND WRITE TO LOCATIONS.CSV LOCALLY
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity != null){
BufferedInputStream bis = new BufferedInputStream(entity.getContent());
filename = "locations.csv";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filename)));
int inByte;
while((inByte = bis.read()) != -1) bos.write(inByte);
bis.close();
bos.close();
}
else if(entity == null) {
System.out.println("NO FILE DOWNLOADED FROM SERVER");
}
} catch (IOException e) {
e.printStackTrace();
}
return null ;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//LOAD CSV FILE
try {
file = new FileReader(filename); //CREATE READ FOR LOCATIONS.CSV FILE A.K.A filename
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
And the resulting LogCat printout:
02-23 00:12:16.550: W/System.err(28119): java.lang.NullPointerException: lock == null
02-23 00:12:16.610: W/System.err(28119): at java.io.Reader.<init>(Reader.java:64)
02-23 00:12:16.610: W/System.err(28119): at java.io.BufferedReader.<init>(BufferedReader.java:92)
02-23 00:12:16.610: W/System.err(28119): at java.io.BufferedReader.<init>(BufferedReader.java:80)
02-23 00:12:16.610: W/System.err(28119): at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper.onCreate(MySQLiteHelper.java:90)
02-23 00:12:16.610: W/System.err(28119): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-23 00:12:16.610: W/System.err(28119): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
02-23 00:12:16.610: W/System.err(28119): at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper.getAllMapMarkers(MySQLiteHelper.java:199)
02-23 00:12:16.610: W/System.err(28119): at uk.ac.aber.dwd.CeredigionTourism.MapActivity.onCreate(MapActivity.java:40)
02-23 00:12:16.610: W/System.err(28119): at android.app.Activity.performCreate(Activity.java:5240)
02-23 00:12:16.610: W/System.err(28119): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-23 00:12:16.610: W/System.err(28119): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-23 00:12:16.610: W/System.err(28119): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-23 00:12:16.610: W/System.err(28119): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-23 00:12:16.610: W/System.err(28119): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-23 00:12:16.610: W/System.err(28119): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 00:12:16.610: W/System.err(28119): at android.os.Looper.loop(Looper.java:137)
02-23 00:12:16.610: W/System.err(28119): at android.app.ActivityThread.main(ActivityThread.java:5048)
02-23 00:12:16.610: W/System.err(28119): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 00:12:16.610: W/System.err(28119): at java.lang.reflect.Method.invoke(Method.java:511)
02-23 00:12:16.610: W/System.err(28119): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
02-23 00:12:16.610: W/System.err(28119): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:675)
02-23 00:12:16.610: W/System.err(28119): at dalvik.system.NativeStart.main(Native Method)
02-23 00:12:17.180: W/System.err(28119): java.io.FileNotFoundException: /res/locations.csv: open failed: EROFS (Read-only file system)
02-23 00:12:17.770: W/System.err(28119): at libcore.io.IoBridge.open(IoBridge.java:416)
02-23 00:12:17.770: W/System.err(28119): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
02-23 00:12:17.770: W/System.err(28119): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
02-23 00:12:17.770: W/System.err(28119): at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper$CSVFileDownloader.doInBackground(MySQLiteHelper.java:296)
02-23 00:12:17.770: W/System.err(28119): at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper$CSVFileDownloader.doInBackground(MySQLiteHelper.java:1)
02-23 00:12:17.770: W/System.err(28119): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-23 00:12:17.770: W/System.err(28119): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-23 00:12:17.770: W/System.err(28119): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-23 00:12:17.770: W/System.err(28119): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-23 00:12:17.770: W/System.err(28119): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-23 00:12:17.770: W/System.err(28119): at java.lang.Thread.run(Thread.java:856)
02-23 00:12:17.780: W/System.err(28119): Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system)
02-23 00:12:17.780: W/System.err(28119): at libcore.io.Posix.open(Native Method)
02-23 00:12:17.780: W/System.err(28119): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
02-23 00:12:17.780: W/System.err(28119): at libcore.io.IoBridge.open(IoBridge.java:400)
02-23 00:12:17.780: W/System.err(28119): ... 10 more
02-23 00:12:18.730: W/System.err(28119): java.io.FileNotFoundException: /res/locations.csv: open failed: ENOENT (No such file or directory)
02-23 00:12:18.780: W/System.err(28119): at libcore.io.IoBridge.open(IoBridge.java:416)
02-23 00:12:18.780: W/System.err(28119): at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-23 00:12:18.780: W/System.err(28119): at java.io.FileInputStream.<init>(FileInputStream.java:105)
02-23 00:12:18.780: W/System.err(28119): at java.io.FileReader.<init>(FileReader.java:66)
02-23 00:12:18.780: W/System.err(28119): at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper$CSVFileDownloader.onPostExecute(MySQLiteHelper.java:319)
02-23 00:12:18.780: W/System.err(28119): at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper$CSVFileDownloader.onPostExecute(MySQLiteHelper.java:1)
02-23 00:12:18.780: W/System.err(28119): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-23 00:12:18.780: W/System.err(28119): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-23 00:12:18.780: W/System.err(28119): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-23 00:12:18.780: W/System.err(28119): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 00:12:18.780: W/System.err(28119): at android.os.Looper.loop(Looper.java:137)
02-23 00:12:18.780: W/System.err(28119): at android.app.ActivityThread.main(ActivityThread.java:5048)
02-23 00:12:18.780: W/System.err(28119): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 00:12:18.780: W/System.err(28119): at java.lang.reflect.Method.invoke(Method.java:511)
02-23 00:12:18.780: W/System.err(28119): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
02-23 00:12:18.780: W/System.err(28119): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:675)
02-23 00:12:18.780: W/System.err(28119): at dalvik.system.NativeStart.main(Native Method)
02-23 00:12:18.780: W/System.err(28119): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
02-23 00:12:18.780: W/System.err(28119): at libcore.io.Posix.open(Native Method)
02-23 00:12:18.780: W/System.err(28119): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
02-23 00:12:18.790: W/System.err(28119): at libcore.io.IoBridge.open(IoBridge.java:400)
02-23 00:12:18.790: W/System.err(28119): ... 16 more
Can anyone point me in the right direction of where I'm going wrong? Not sure if the file is even downloading and definitely don't know why it can't be found as a resource?!
java.io.FileNotFoundException: /res/locations.csv
Its trying to find the file locations.csv in the resources folder of your app. You need to specify the path of sdcard in case you want to store the file on the device.
FileOutputStream outputStream;
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
// Now you can wrap this outputstream with BufferedOutputStream
//and copy contents of the file that you are getting from the website
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
int inByte;
while((inByte = bis.read()) != -1) bos.write(inByte);
bis.close();
bos.close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.