0

AsyncTask の IllegalStateExceptionに関する私の質問を参照してください。

このタスクの 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 がスローされます。なぜこれが起こるのか、これを修正する方法は?

ありがとう

4

0 に答える 0