2013-01-04 99 views
0

從服務器下載圖像時發生此IOException錯誤,導致應用程序崩潰。這隻會在一段時間內發生,通常在下載時不會發生錯誤。我不確定是什麼原因導致這種情況發生。如果有人可以在這個問題上解釋,這將是一個很大的幫助。先謝謝你!InputStream發生IOException

logcat的

01-03 17:23:13.136: E/AndroidRuntime(12597): java.lang.RuntimeException: An error occured while executing doInBackground() 
01-03 17:23:13.136: E/AndroidRuntime(12597): at android.os.AsyncTask$3.done(AsyncTask.java:200) 
01-03 17:23:13.136: E/AndroidRuntime(12597): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
01-03 17:23:13.136: E/AndroidRuntime(12597): at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
01-03 17:23:13.136: E/AndroidRuntime(12597): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
01-03 17:23:13.136: E/AndroidRuntime(12597): at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
01-03 17:23:13.136: E/AndroidRuntime(12597): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
01-03 17:23:13.136: E/AndroidRuntime(12597): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
01-03 17:23:13.136: E/AndroidRuntime(12597): at java.lang.Thread.run(Thread.java:1019) 
01-03 17:23:13.136: E/AndroidRuntime(12597): Caused by: java.lang.NullPointerException 
01-03 17:23:13.136: E/AndroidRuntime(12597): at  com.f.bb.GalleryScreen$DownloadFile.doInBackground(GalleryScreen.java:242) 
01-03 17:23:13.136: E/AndroidRuntime(12597): at com.f.bb.GalleryScreen$DownloadFile.doInBackground(GalleryScreen.java:1) 
01-03 17:23:13.136: E/AndroidRuntime(12597): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
01-03 17:23:13.136: E/AndroidRuntime(12597): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
01-03 17:23:13.136: E/AndroidRuntime(12597): ... 4 more 
01-03 17:23:20.792: E/WindowManager(12597): Activity com.f.bb.GalleryScreen has leaked window [email protected] that was originally added here 
01-03 17:23:20.792: E/WindowManager(12597): android.view.WindowLeaked: Activity com.f.bb.GalleryScreen has leaked window [email protected] that was originally added here 
01-03 17:23:20.792: E/WindowManager(12597):  at android.view.ViewRoot.<init>(ViewRoot.java:263) 
01-03 17:23:20.792: E/WindowManager(12597):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:171) 
01-03 17:23:20.792: E/WindowManager(12597):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114) 
01-03 17:23:20.792: E/WindowManager(12597):  at android.view.Window$LocalWindowManager.addView(Window.java:424) 
01-03 17:23:20.792: E/WindowManager(12597):  at android.app.Dialog.show(Dialog.java:241) 
01-03 17:23:20.792: E/WindowManager(12597):  at com.f.bb.GalleryScreen$DownloadFile.onPreExecute(GalleryScreen.java:272) 
01-03 17:23:20.792: E/WindowManager(12597):  at android.os.AsyncTask.execute(AsyncTask.java:391) 
01-03 17:23:20.792: E/WindowManager(12597):  at com.f.bb.GalleryScreen$1$1.run(GalleryScreen.java:121) 
01-03 17:23:20.792: E/WindowManager(12597):  at android.app.Activity.runOnUiThread(Activity.java:3717) 
01-03 17:23:20.792: E/WindowManager(12597):  at com.f.bb.GalleryScreen$1.onClick(GalleryScreen.java:85) 
01-03 17:23:20.792: E/WindowManager(12597):  at android.view.View.performClick(View.java:2538) 
01-03 17:23:20.792: E/WindowManager(12597):  at   android.view.View$PerformClick.run(View.java:9152) 
01-03 17:23:20.792: E/WindowManager(12597):  at android.os.Handler.handleCallback(Handler.java:587) 
01-03 17:23:20.792: E/WindowManager(12597):  at android.os.Handler.dispatchMessage(Handler.java:92) 
01-03 17:23:20.792: E/WindowManager(12597):  at  android.os.Looper.loop(Looper.java:130) 
01-03 17:23:20.792: E/WindowManager(12597):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
01-03 17:23:20.792: E/WindowManager(12597):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-03 17:23:20.792: E/WindowManager(12597):  at java.lang.reflect.Method.invoke(Method.java:507) 
01-03 17:23:20.792: E/WindowManager(12597):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
01-03 17:23:20.792: E/WindowManager(12597):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
01-03 17:23:20.792: E/WindowManager(12597):  at dalvik.system.NativeStart.main(Native Method) 
01-03 17:27:16.148: I/Process(12597): Sending signal. PID: 12597 SIG: 9 

代碼(的AsyncTask)

private class DownloadFile extends AsyncTask<String, Integer, String>{ 
    @Override 
    protected String doInBackground(String... sUrl){ 

     Bitmap bm; 
     InputStream in; 
     if(downloadFailed == false){ 
      if (contentId != null && contentId.length>0){ 
       Log.i(TAG, "contentid.length:" +contentId.length); 
       for (int i=0;i<contentId.length;i++){ 
        if(helper.databaseChecking(useremail, contentId[i])){ 
         Constant.CONTENT_ID = contentId[i]; 
         Log.i(TAG, "contentId:" +contentId[i]); 

         Log.i(TAG, "Email:" +useremail); 
         Log.i(TAG, "Section ID:" +ssID); 

         downloadList = new String[contentId.length]; 


         try{ 
          in = new java.net.URL(downloadList[i]).openStream(); 
          bm = BitmapFactory.decodeStream(new PatchInputStream(in)); 
          File storage = new File(Environment.getExternalStorageDirectory() + File.separator + "/testing/"); 
          Log.i(TAG,"storage:" +storage); 
          Log.i(TAG,"storage:" +storage.getAbsolutePath()); 
          if(!storage.exists()){ 
           storage.mkdirs(); 
           } 
          String FileName = "/"+Constant.CONTENT_ID+".jpg"; 
          FileOutputStream fos = new FileOutputStream(storage + FileName); 
          bm.compress(Bitmap.CompressFormat.JPEG, 85, fos); 
          String filepath = storage + FileName; 
          File filecheck = new File (filepath); 
          long fileSize = filecheck.length(); 
          fos.flush(); 
          fos.close(); 


          Constant.IMAGE_EXIST = 1; 
          } 
         catch(IOException e1){ 
         e1.printStackTrace(); 
         downloadFailed = true; 
         } 

        } 

       } 
      } 
     } 
     return null; 
    } 
    @Override 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     pDialog.setMessage("Downloading file. Please wait..."); 
     pDialog.show(); 
    } 


    protected void onPostExecute(String result){ 
     super.onPostExecute(result); 
     pDialog.dismiss(); 

     alertTitle = "Information"; 
     alertMessage = "Sync Completed. Click 'OK' to refresh page"; 
     dialogBox_intent(); 
     Constant.SYNC_COMPLETE = true; 


    } 
} 
+0

的NullPointerException GalleryScreen.java 242是此行'bm.compress(Bitmap.CompressFormat.JPEG,85%,FOS);' –

+0

嘗試使用這些'字符串文件名= Constant.CONTENT_ID + 「JPG」;'。當你幹運行時,你發現'/ mnt/sdcard/testing' //''Constant.CONTENT_ID.jpg'導致寫入文件。希望它可以工作 –

+0

以前,我嘗試了多次嘗試將文件寫入手機內存。但是遇到了很多問題和問題,最後它以這條路線而告終,並且工作起來。你的意思是它會覆蓋我的文件? –

回答

1

BitmapFactory.decodeStream()可以返回null。請參閱documentation,但有一種情況是下載的圖像已損壞。應測試使用任何方法bm != null之前,如bm.compress(...)

+0

我想我明白你的意思了,我可以用bm!= null來檢查文件是否損壞。如果它損壞,它將返回爲空。 –

+0

是的,在'bm = BitmapFactory.decodeStream()'後,做一個'if(bm!= null){...圖像很好...}其他{...圖像不好...} – iagreen

+0

你的答案。我認爲這將解決我的問題。 –

相關問題