图库显示强制关闭错误

时间:2012-03-21 12:02:54

标签: android android-2.2-froyo

我创建了一个应用程序,其中我创建了库和下面的库我在缩放视图中显示所选图像...当我有超过2个图像时,我的图库显示强制关闭错误当我点击图像时..我需要帮助...

我的应用程序代码是:

        img = (ImageView) findViewById(R.id.GalleryView);

     DisplayMetrics metrics = new DisplayMetrics();
     getWindowManager().getDefaultDisplay().getMetrics(metrics);

    Gallery g = (Gallery) findViewById(R.id.gallery);

     // set gallery to left side
    MarginLayoutParams mlp = (MarginLayoutParams) g.getLayoutParams();
    mlp.setMargins((int)-(metrics.widthPixels / 2 + 35), mlp.topMargin,mlp.rightMargin, mlp.bottomMargin);
    imageAdapter = new ImageAdapter(this,imgarr);
    g.setAdapter(imageAdapter);

    if(imgarr.length > 1)
    {       
        img.setImageURI(Uri.parse(imgarr[0]));          
    }
    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            dialog = ProgressDialog.show(ImageGallery.this,
                    "Loading Image", "Please wait...", true);
            img.setImageURI(Uri.parse(imgarr[position]));               
            dialog.dismiss();
        }
    });

 ImageAdapter class:
        public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    String[] imgArray;
    public ImageAdapter(Context c,String[] imgArray) {
        mContext = c;
        TypedArray attr = mContext
                .obtainStyledAttributes(R.styleable.ImageGallery);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.ImageGallery_android_galleryItemBackground, 0);
        this.imgArray = imgArray;
        attr.recycle();
    }

    public int getCount() {
        return imgArray.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        //Toast.makeText(getApplicationContext(), "Image path from gallery : " + imgArray[position],
        //Toast.LENGTH_SHORT).show();
        //Bitmap bitmap = BitmapFactory.decodeFile(imgArray[position]);
        Log.d("cursor lengh :", "" +imgArray[position]);
        //i.setImageBitmap(bitmap);
        i.setImageURI(Uri.parse(imgArray[position]));
        i.setLayoutParams(new Gallery.LayoutParams(80, 70));
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setBackgroundResource(mGalleryItemBackground);
        return i;
    }
}

我有这些错误: enter image description here

1 个答案:

答案 0 :(得分:0)

我认为你在这一行上遇到了错误。

   img.setImageURI(Uri.parse(imgarr[position]));       

错误VM Budget表示图像分辨率高,尺寸太大而无法容纳。

Sol:您需要缩小图像的大小。

您可以使用inSampleSizecreateScaledBitmapthis link

同时了解如何使用视图并释放其内存以提高效率。

快乐编码:)