2012-03-21 13 views
0

ギャラリーを作成してギャラリーを作成したアプリケーションを1つ作成しました。ズームビューで選択した画像を表示しています... 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

+0

ここでlogcat画像を貼り付ける代わりに、logcatログ自体を貼り付けることができますか?大いに役立つでしょう。可視性の問題。私は 'OutOfMemoryError:ビットマップサイズがVM予算を超えています.'しか見ることができません。これはあなたのビットマップが巨大であることを意味します。 – Ghost

答えて

0

私はあなたがこの行にエラーが出ていると思います。

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

エラーVMバジェットは、画像が高解像度であり、サイズが大きすぎて収容することができないことを示します。

ソル:画像のサイズを小さくする必要があります。

あなたはまた、ビューを使用してefficiancyために、そのメモリを解放する方法を理解inSampleSizeまたはcreateScaledBitmapまたは

this linkを使用することができます。

ハッピーコーディング:)

+0

私はinSampleSizeを使用していますが、onActivityResultメソッドのsdcardに画像を保存しています...そして、私はリンクのコードを試しました。まだこのエラーが来ています...私のコードは: BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 3; bitmap = BitmapFactory.decodeFile(path、options); –

関連する問題