从Sdcard异步解码图像 - 图像不显示

时间:2013-12-26 09:25:56

标签: android image gridview asynchronous bitmapfactory

我一直在努力想要发现如何从我的内部存储中显示图像而没有滞后和错误......我用Google搜索了很多,并且对我以前的问题得到了一些答案,但现在我我真的很绝望。

所以这是我的问题,我想创建一个类似app的图库,使用简单的gridView和imageViews,但是,当我第一次启动应用程序时,网格看起来是空的(我在gridview中有100个图像用于测试)当我滚动时,图像向下显示,当我向后滚动到开头时,图像再次出现!我尝试修改我的代码,结果是它们出现了,但是应用程序滞后了,因为它没有异步解码...以下是相关的方法和内容:

我的onCreate():

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);

        TwoWayGridView grid = (TwoWayGridView) findViewById(R.id.gridview);
        grid.setNumColumns(2);
        grid.setAdapter(new GirdAdapter());

    }

我的适配器:

class GirdAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 100;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {

            RelativeLayout r = (RelativeLayout) getLayoutInflater().inflate(R.layout.image_view, null); // Just a conatianer
            ImageView image = (ImageView) r.findViewById(R.id.imageView1);
            ImageButton imageButton = (ImageButton) r.findViewById(R.id.imageButton1);

            imageButton.setVisibility(View.GONE); // I don't need this button now


            String pic = "/storage/emulated/0/DCIM/Camera/IMG_20131225_183622.jpg" ; // This is an absolute path to an image on my internal storage

            image.setImageBitmap(getProperBitmap(pic, 200, 200 , GalleryActivity.this)); 

            return r;
        }

    }

异步解码方法:

public static Bitmap getProperBitmap(final String imagePath , final int reqHeight , final int reqWidth ){



        AsyncTask<String, String, Bitmap> background = new AsyncTask<String, String, Bitmap>() {

            @Override
            protected Bitmap doInBackground(String... params) {

                BitmapFactory.Options options = new Options();

                options.inJustDecodeBounds = true ;

                BitmapFactory.decodeFile(imagePath, options);

                int height = options.outHeight ;
                int width = options.outWidth ;

                int inSampleSize = 1;

                if (height > reqHeight || width > reqWidth) {

                    final int halfHeight = height / 2;
                    final int halfWidth = width / 2;

                    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                    // height and width larger than the requested height and width.
                    while ((halfHeight / inSampleSize) > reqHeight
                            && (halfWidth / inSampleSize) > reqWidth) {
                        inSampleSize *= 2;
                    }


                }

                options.inSampleSize = inSampleSize ;

                options.inJustDecodeBounds = false ;



                result = BitmapFactory.decodeFile(imagePath, options);

                return result;
            }

            @Override
            protected void onPostExecute(Bitmap result) {

                GalleryActivity.result = result ;

                super.onPostExecute(result);
            }


        }.execute("");


        return result ;
    }

1 个答案:

答案 0 :(得分:0)

代码几乎是稳定的,但是,为了显示图像,一旦AsyncTask完成,你必须调用image.setImageBitmap ...

为此,您必须向getProperBitmap方法添加ImageView参数,并在调用此方法时,分配您要使用的ImageView ...

所以它会是这样的:

public static Bitmap getProperBitmap(final String imagePath , final int reqHeight , final int reqWidth , ImageView v)

然后,在异步任务结束时,在onPostExecute中,设置图像!所以它会像:

@Override
            protected void onPostExecute(Bitmap result) {

                GalleryActivity.result = result ;

                v.setImageBitmap(result) ;

                super.onPostExecute(result);
            }

有了这个,一切都会好起来的!