在android中动态添加画廊图片

在android中动态添加画廊图片,android,android-gallery,Android,Android Gallery,我已经完成了emulator上静态显示的图像库(即来自drawable文件夹的图像)。现在我需要从本地路径(对于ex.from e:/anim.jpeg)将一些图像动态添加到库列表中。我该如何做?谢谢 我的图库代码如下所示 public class GalleryAct extends Activity { private Gallery gallery; private ImageView imgView; private Integer[] Imgid = { R.dra

我已经完成了emulator上静态显示的图像库(即来自
drawable
文件夹的图像)。现在我需要从本地路径(对于ex.from e:/anim.jpeg)将一些图像动态添加到库列表中。我该如何做?谢谢

我的图库代码如下所示

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

}
公共类GalleryAct扩展活动{
私人画廊;
私有图像视图imgView;
私有整数[]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
};
@凌驾
创建时的公共void(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(新AddImgAdp(本));
gallery.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
imgView.setImageResource(Imgid[position]);
}
});
}
公共类addimgapp扩展BaseAdapter{
int GalItemBg;
私人语境控制;
公共AddImgAdp(上下文c){
cont=c;
TypedArray typArray=获取StyledAttributes(R.styleable.GalleryTheme);
GalItemBg=typArray.getResourceId(R.styleable.GalleryTheme\u android\u galleryItemBackground,0);
typArray.recycle();
}
public int getCount(){
返回Imgid.length;
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
ImageView imgView=新的ImageView(续);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(新图库.LayoutParams(80,70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
返回imgView;
}
}
}

写入保存图像的文件路径

getExternalStorageDirectory()提供SD卡的路径

  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;
}
位图位图=解码文件(f1);
imgView.setImageBitmap(位图);
私有位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//我们要扩展到的新尺寸
所需的最终int_尺寸=150;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp/2<所需尺寸| |高度\u tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeStream(新文件输入流(f),null,o2);
}catch(filenotfounde异常){
}
返回null;
}

写入保存图像的文件路径

getExternalStorageDirectory()提供SD卡的路径

  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;
}
位图位图=解码文件(f1);
imgView.setImageBitmap(位图);
私有位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//我们要扩展到的新尺寸
所需的最终int_尺寸=150;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp/2<所需尺寸| |高度\u tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeStream(新文件输入流(f),null,o2);
}catch(filenotfounde异常){
}
返回null;
}

在您的情况下,您可以尝试将图像数组设置为动态列表,例如: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;
    }
}
公共类addimgapp扩展BaseAdapter{
int GalItemBg;
ArrayList位图列表;
私人语境控制;
公共AddImgAdp(上下文c、ArrayList位图列表){
cont=c;
TypedArray typArray=获取StyledAttributes(R.styleable.GalleryTheme);
GalItemBg=typArray.getResourceId(R.styleable.GalleryTheme\u android\u galleryItemBackground,0);
typArray.recycle();
this.bitmapList=bitmapList;
}
public int getCount(){
返回bitmapList.size();
}
公共对象getItem(int位置){
返回bitmapList.get(位置);
}
公共长getItemId(int)