在Android的动态添加图库图片 [英] Add Gallery images dynamically in android

查看:239
本文介绍了在Android的动态添加图库图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了静态显示在模拟器影像画廊(即从图像绘制文件夹)。现在我需要一些图像添加到库列表中,动态从本地路径( ex.from E:/anim.jpeg 这样的)?。如何我可以做到这一点谢谢..

我的展厅code如下图所示。

 公共类GalleryAct延伸活动{

私人画廊的画廊;
私人ImageView的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
};

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

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

     画廊=(图库论坛)findViewById(R.id.examplegallery);
     gallery.setAdapter(新AddImgAdp(本));

     gallery.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
            imgView.setImageResource(Imgid [位置]);
        }
    });

}

公共类AddImgAdp扩展了BaseAdapter {
    INT GalItemBg;
    私人语境续;

    公共AddImgAdp(上下文C){
        CONT = C;
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground,0);
        typArray.recycle();
    }

    公众诠释getCount将(){
        返回Imgid.length;
    }

    公共对象的getItem(INT位置){
        返回的位置;
    }

    众长getItemId(INT位置){
        返回的位置;
    }

    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        ImageView的imgView =新ImageView的(续)

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

        返回imgView;
    }
}

}
 

解决方案

写在图像的保存文件路径。

Environment.getExternalStorageDirectory()给出了SD卡的路径。

 文件F1 =新的文件(Environment.getExternalStorageDirectory()
                文件分割符+ +test2.png);


 BitmapFactory.Options O =新BitmapFactory.Options();
 o.inJustDe codeBounds = TRUE;
 点阵位图= BitmapFactory.de codeStream(新的FileInputStream(f)项,空,O);

 imgView.setImageBitmap(位);
 

如果你的图片太大的大小比位图将报错所以对你有以下code写来调整图像。传递文件在下面的功能

 位图位=去codeFILE(F1);
 imgView.setImageBitmap(位);

 私人位图德codeFILE(文件f){
    尝试 {
        //德code图像尺寸
        BitmapFactory.Options O =新BitmapFactory.Options();
        o.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeStream(新的FileInputStream(f)项,空,O);

        //我们希望新的大小扩展到
        最终诠释REQUIRED_SIZE = 150;

        //找到正确的比例值。它应该是2的幂。
        INT width_tmp = o.outWidth,height_tmp = o.outHeight;
        int标= 1;
        而(真){
            如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2'; REQUIRED_SIZE)
                打破;
            width_tmp / = 2;
            height_tmp / = 2;
            规模* = 2;
        }

        //德code与inSampleSize
        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;
        返回BitmapFactory.de codeStream(新的FileInputStream(f)项,空,O2);

    }赶上(FileNotFoundException异常E){
    }
    返回null;
}
 

I've done Gallery of images displayed on emulator by static(i.e images from drawable folder).Now i need to add some images into the list of gallery, dynamically from the local path(for ex.from E:/anim.jpeg like that).How can i do that?Thanks..

My gallery code is shown below..

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

}

解决方案

Write the file path where the image is saved.

Environment.getExternalStorageDirectory() gives path of 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);

If your image is too big size than bitmap will give error so for that you have to write below code to resize image. Pass the file in below function

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

这篇关于在Android的动态添加图库图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆