2011-01-06 50 views
1

我已經完成了靜態圖像(例如drawable文件夾中的圖像)顯示的圖像庫。現在我需要從本地路徑動態地添加一些圖像到圖庫列表中(對於ex.from E:?/anim.jpeg這樣)我。如何能做到這一點謝謝..在android中動態添加圖庫圖像

我的展廳代碼如下所示..

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

謝謝你的回覆shah。 – sanjay 2011-01-06 13:20:28

3

在你的情況,你可以嘗試讓你的圖像數組動態列表,例如:ArrayList。在新項目到來時,將其添加到列表中,並調用notifyDataSetChanged()(適配器的方法),並且您的圖庫列表將被刷新。

取決於你的情況,我發現最好在這裏使用AsyncTask來更新列表並調用notifyDataSetChanged。

適配器類同樣也會看起來這個

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; 
    } 
} 

讓我知道如果任何錯誤,我的方式取決於IDE。