AsyncTask下载图片中的异常

时间:2012-09-04 11:07:37

标签: android exception android-asynctask

IllegalStateException in AsyncTask上提及我的问题。

我从此任务的onPostExecute()调用了以下AsyncTask(在此链接中引用)。

    public class SaveImageAsync extends AsyncTask<Context, Integer, String> {
        private Bitmap bitmap = null;
        private ArrayList<String> imageUrls = null;
        private ArrayList<ImageView> imageViews = null;
        int index = 0;

        public SaveImageAsync(ArrayList<ImageView> imageViews,
                ArrayList<String> imageUrls) {
            this.imageUrls = imageUrls;
            this.imageViews = imageViews;
            }
    @Override
    protected String doInBackground(Context... params) {
        boolean bRC;

        String imageName, imageLocalPath, imageURL;

        try {

            for (int i = 0; i < imageUrls.size(); i++) {

                imageURL = imageUrls.get(i);
                String[] strSplittedImagePath = imageURL
                        .split("/");
                if ((strSplittedImagePath != null)
                        && (strSplittedImagePath.length >= 1)) {
                    imageName = strSplittedImagePath[(strSplittedImagePath.length - 1)];
                } else {
                    imageName = imageURL;
                }

                imageLocalPath = path + imageName;

                bRC = doesFileExists(imageLocalPath);

                if (bRC == false) {
                    getImageFromServer(CommonSettings.mSTstrBaseURL
                            + imageURL, imageLocalPath);
                }

                try {
                    bitmap = BitmapFactory
                            .decodeFile(imageLocalPath);
                } catch (Exception e) {
                }

                if (bitmap == null) {
                    try {
                        new File(imageLocalPath).delete();
                    } catch (Exception e1) {
                    }
                    getImageFromServer(CommonSettings.mSTstrBaseURL
                            + imageURL, imageLocalPath);
                    bitmap = BitmapFactory
                            .decodeFile(imageLocalPath);
                }

                Thread.sleep(300);
                publishProgress();
            }
        } catch (InterruptedException e) {

            e.printStackTrace();
            Log.w(LOG_TAG,
                    "Got InterruptedException inside AsyncTask SaveImageAsync   :   "
                            + e);

        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        ImageView imageView = imageViews.get(index);
        while (imageView == null) {
            index = index + 1;
            imageView = imageViews.get(index);
        }
        imageView.setScaleType(ScaleType.FIT_XY);
        imageView.setImageBitmap(bitmap);
        index = index + 1;
        //bitmap.recycle();
    }


    public boolean getImageFromServer(String url, String localPath) {
        boolean bReturn = false;

        File objNewFile;
        try {
            objNewFile = new File(localPath);
            if (!objNewFile.exists()) {
                objNewFile.createNewFile();
            }

            BufferedInputStream objBufferedInput = new BufferedInputStream(
                    new java.net.URL(url).openStream());
            FileOutputStream objFileOutput = new FileOutputStream(objNewFile);
            BufferedOutputStream objBufferOutput = new BufferedOutputStream(
                    objFileOutput, 1024);
            byte[] data = new byte[1024];
            int x = 0;
            while ((x = objBufferedInput.read(data, 0, 1024)) >= 0) {
                objBufferOutput.write(data, 0, x);
            }
            objFileOutput.flush();
            objBufferOutput.flush();
            objFileOutput.close();
            objBufferOutput.close();
            objBufferedInput.close();
            bReturn = true;
        } catch (IOException e) {

        } catch (Exception ex) {
        }
        return bReturn;
    }
}

当按下后退按钮并加载此任务时,它会在decodeFile()处抛出OutOfMemoryException。为什么会发生这种情况以及如何解决这个问题?

谢谢

0 个答案:

没有答案