android - 在android中动态添加图库图片

标签 android android-gallery

我已经通过静态(即来自 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;
    }
}

}

最佳答案

写入图片保存的文件路径。

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

关于android - 在android中动态添加图库图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4613668/

相关文章:

android - 如何在没有按钮的情况下关闭 AlertDialog.Builder

android - 在图库 View (及其 subview )之上自定义绘图

android - 如何获取从图库中选择的 Picasa 图片的缩略图?

java - 无限无尽的画廊?好吧,它不适合我

android - 获取当前图片在图库中显示的位置

java - libgdx - gameoverscreen 不会显示

android - 附加 Parcelable 的尺寸限制?

java - Cordova 3,无法开始 Activity

android - gradle 和 gradle-experimental 有什么区别?

java - 光标错误 - 从图库中检索图像