0

从服务器下载图像时出现此 IOException 错误,它使我的应用程序崩溃。这只会偶尔发生一次,通常在下载时不会发生错误。我不确定是什么原因造成的,以及为什么会发生这种情况。如果有人可以解释这个问题,那将是一个很大的帮助。先感谢您!

日志猫

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 com.android.internal.policy.impl.PhoneWindow$DecorView@405b3e78 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 com.android.internal.policy.impl.PhoneWindow$DecorView@405b3e78 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

代码(异步任务)

    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;


    }
}
4

1 回答 1

1

BitmapFactory.decodeStream()可以返回null。请参阅文档,但一种情况是下载的图像已损坏。您应该bm != null在使用任何方法之前进行测试,例如bm.compress(...)

于 2013-01-04T05:40:43.987 回答