如果使用imageGetter发生任何异常,如何重新加载图像

时间:2015-11-16 10:12:31

标签: android html bitmap android-asynctask

我正在尝试使用img>解析< ImageGetter标记中的图像。我成功地解析并下载了图像。但是,在最坏的情况下,如no coonectivity, low network,如果未加载图像,我想重新加载图像。根据我的理解,当位图为空时,我会在onPostExecute() Asynctask方法中重新加载图像。但为此,我必须再次调用Asynctask方法。还有其他替代方法可以重新加载图像。

以下是我的Imagegetter代码:

public class UrlImageParser implements Html.ImageGetter {
    private static String TAG = "ImageParser";

    private TextView mContainer;
    private Context mContext;
    Point outSize=new Point();
    float  destWidth=1;
    float  destHeight=1;

    public UrlImageParser(TextView t, Context context) {
        mContainer = t;
        mContext = context;

    }

    @Override
    public Drawable getDrawable(String source) {
        LevelListDrawable d = new LevelListDrawable();
        Drawable empty = mContext.getResources().getDrawable(R.drawable.story_img_placeholder);
        d.addLevel(0, 0, empty);
        d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());

        new LoadImage().execute(source, d);

        return d;
    }




    class LoadImage extends AsyncTask<Object, Void, Bitmap> {

        private LevelListDrawable mDrawable;

        @Override
        protected Bitmap doInBackground(Object... params) {
            String source = (String) params[0];
            mDrawable = (LevelListDrawable) params[1];
            Log.d(TAG, "doInBackground " + source);
            try {
                InputStream is = new URL(source).openStream();
                return BitmapFactory.decodeStream(is);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();

            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            Log.d(TAG, "onPostExecute drawable " + mDrawable);
            Log.d(TAG, "onPostExecute bitmap " + bitmap);
            if (bitmap != null) {
                BitmapDrawable d = new BitmapDrawable(scaleBitmap(bitmap));
                mDrawable.addLevel(1, 1, d);
                /*int width = bitmap.getWidth();
                int screenWidth = Utility.getScreenWidth(mContext);
                int height = (int) (screenWidth * 0.62);*/
                mDrawable.setBounds(0, 0, d.getBitmap().getWidth(), d.getBitmap().getHeight());
                mDrawable.setLevel(1);
                    /*// redraw the image by invalidating the container
                        UrlImageParser.this.container.invalidate(); */

                if (mContainer != null) {
                    mContainer.setText(mContainer.getText());
                }

            }else{
                //Some error occured, send the request again
            }
        }


    }

    private Bitmap scaleBitmap(Bitmap mFile){
        Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        if (android.os.Build.VERSION.SDK_INT >= 13){
            display.getSize(outSize);
            destWidth = outSize.x;
            destHeight = outSize.y;
        }else{
            destWidth=display.getWidth();
            destHeight=display.getHeight();
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap orig = mFile;
        float srcWidth = orig.getWidth();
        float srcHeight = orig.getHeight();
        Bitmap resized=Bitmap.createScaledBitmap(orig, (int)Math.round(destWidth), (int)Math.round(destWidth * srcHeight /srcWidth), true);
        destWidth=1;
        destHeight=1;
        return resized;
    }
}

1 个答案:

答案 0 :(得分:1)

执行此操作的最佳方法是永远不要离开doInBackground()或再次启动整个AsyncTask。你可以这样做:

 int failureCounter = 0;

 @Override
 protected Bitmap doInBackground(Object... params) {
     try {
         String source = (String) params[0];
         mDrawable = (LevelListDrawable) params[1];
         Log.d(TAG, "doInBackground " + source);
         InputStream is = new URL(source).openStream();
         return BitmapFactory.decodeStream(is);
     } catch (Exception e) {
         e.printStackTrace();
         if(this.failureCounter++ >= 5) {
             return null;
         } else {
             return this.doInBackground(params);
         }
     } 
 }

此代码段将在返回null之前重试加载图像5次。您应该限制阻止StackOverflowError的尝试次数,并限制任务运行的时间,因为AsyncTasks不是长时间运行的后台任务。