2014-02-23 73 views
0

因此,我想從永久URL下載文件,然後將其保存在本地,以便稍後可以訪問它以寫入SQLite數據庫。我有一個處理此功能的類AsynchTask,但我一直會收到FileNotFoundException錯誤(請參閱Logcat)。從URL下載文件時引發FileNotFoundException

下載CSV文件類

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(); 
      } 

     } 
    } 

,並將所得logcat的打印:

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 

任何人都可以點我在我要去錯在何處的方向是正確的?不知道該文件是否是下載,而且絕對不知道爲什麼它不能作爲資源被發現?!

回答

0

java.io.FileNotFoundException:/res/locations.csv

它試圖找到在您的應用程序的資源文件夾中的文件locations.csv。如果要將文件存儲在設備上,您需要指定sdcard的路徑。

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(); 
+0

我的平板電腦沒有SD卡。我想將文件存儲在應用程序本身中,因爲我只希望此應用程序將此文件用作資源。 – user3319818

+0

我修改了它,現在'BufferedReader'被指定爲'NullPointerException' – user3319818

+0

請參考以下URL中的「保存到內部存儲」一節中提到的代碼:http://developer.android.com/training/basics/數據存儲/ files.html – Kakarot