2011-01-06 17 views
1

私はエミュレータで表示された画像のギャラリーを静的(つまりdrawableフォルダの画像)しました。ギャラリーのリストにギャラリーを追加する必要があります(ex.fromのE:?/anim.jpegような).Howは、私はそれを行うことができますおかげで..ギャラリーの画像をアンドロイドで動的に追加する

マイギャラリーコードを以下に示します。..

public class GalleryAct extends Activity { 

private Gallery gallery; 
private ImageView imgView; 

private Integer[] Imgid = { 
     R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, 
     R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7 
}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    imgView = (ImageView)findViewById(R.id.ImageView01);  
    imgView.setImageResource(Imgid[0]); 

    gallery = (Gallery) findViewById(R.id.examplegallery); 
    gallery.setAdapter(new AddImgAdp(this)); 

    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      imgView.setImageResource(Imgid[position]); 
     } 
    }); 

} 

public class AddImgAdp extends BaseAdapter { 
    int GalItemBg; 
    private Context cont; 

    public AddImgAdp(Context c) { 
     cont = c; 
     TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme); 
     GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0); 
     typArray.recycle(); 
    } 

    public int getCount() { 
     return Imgid.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 imgView = new ImageView(cont); 

     imgView.setImageResource(Imgid[position]); 
     imgView.setLayoutParams(new Gallery.LayoutParams(80, 70)); 
     imgView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imgView.setBackgroundResource(GalItemBg); 

     return imgView; 
    } 
} 

} 

答えて

4

はイメージがあるファイルパスを書きます保存されました。

Environment.getExternalStorageDirectory()は、sdcardのパスを指定します。

File f1 = new File(Environment.getExternalStorageDirectory() 
       + File.separator + "test2.png"); 


BitmapFactory.Options o = new BitmapFactory.Options(); 
o.inJustDecodeBounds = true; 
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

imgView.setImageBitmap(bitmap); 

画像がビットマップと比べて大きすぎる場合はエラーが発生しますので、画像のサイズを変更するコードを以下に記述する必要があります。ファイルを関数の下に渡します。

Bitmap bitmap = decodeFile(f1); 
imgView.setImageBitmap(bitmap); 

private Bitmap decodeFile(File f) { 
    try { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE = 150; 

     // Find the correct scale value. It should be the power of 2. 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 

    } catch (FileNotFoundException e) { 
    } 
    return null; 
} 
+0

返信いただきありがとうございます。 – sanjay

+0

こんにちは..私は上記の答えでチェックしました。ありがとうございます。しかし、それはパスから内部的に画像を取り、ビューアに画像を表示しました。しかし、私は参照ボタンを作成する必要があります。ユーザーがエミュレータのボタンをクリックすると、 open.itは、同じアプリケーションのdrawbleフォルダにある画像のリストを表示します。それから、私はthat.itから1つの画像を選択してギャラリーリストに表示する必要があります(メールに添付されたファイルのように)。 ?アドバイスありがとうございます。 – sanjay

3

あなたの場合、イメージ配列を動的リスト、ex:ArrayListにすることができます。新しい項目が到着したら、それをリストに追加し、notifyDataSetChanged()(アダプタのメソッド)を呼び出すと、ギャラリーリストがリフレッシュされます。

場合によっては、ここでAsyncTaskを使用してリストを更新し、notifyDataSetChangedを呼び出す方がよいことがわかりました。

アダプタクラスは、何らかのエラーが、私はIDEに依存への道だ場合、私に教えてください。この

public class AddImgAdp extends BaseAdapter { 
    int GalItemBg; 
    ArrayList<Bitmap> bitmapList; 
    private Context cont; 

    public AddImgAdp(Context c, ArrayList<Bitmap> bitmapList) { 
     cont = c; 
     TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme); 
     GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0); 
     typArray.recycle(); 
     this.bitmapList = bitmapList; 
    } 

    public int getCount() { 
     return bitmapList.size(); 
    } 

    public Object getItem(int position) { 
     return bitmapList.get(position); 
    } 

    public long getItemId(int position) { 
     return bitmapList.get(position); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imgView = new ImageView(cont); 

     // imgView.setImageResource(Imgid[position]); 
     imgView.setImageBitmap(bitmapList.get(position)); 

     imgView.setLayoutParams(new Gallery.LayoutParams(80, 70)); 
     imgView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imgView.setBackgroundResource(GalItemBg); 

     return imgView; 
    } 
} 

と同様に見えるでしょう。

関連する問題