【问题标题】:How to reload images, if any exception ocurrs using imageGetter如果使用 imageGetter 发生任何异常,如何重新加载图像
【发布时间】:2015-11-16 10:12:30
【问题描述】:

我正在尝试使用 ImageGetter 解析 img> 标签中的图像。我能够成功地解析和下载图像。但是,在最坏的情况下,比如no coonectivity, low network,如果图像没有加载,我想重新加载图像。据我了解,当位图为空时,我在AsynctaskonPostExecute() 方法中重新加载图像。但为此,我必须再次调用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;
    }
}

【问题讨论】:

    标签: android html bitmap android-asynctask


    【解决方案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);
             }
         } 
     }
    

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

    【讨论】:

    • Wurthner,谢谢你帮助我。但我尝试使用 Volley 自定义 ImageRequest 类。在 OnError() 方法中,我正在检查刷新计数并设置计时器,然后调用图像请求方法。
    猜你喜欢
    • 2014-05-15
    • 2011-05-08
    • 1970-01-01
    • 2023-03-24
    • 2018-11-28
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多