如何显示在Android的图片廊 [英] How to display Image Gallery in Android

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

问题描述

我是新android开发。 我的问题是我能够检索源码数据库中的数据。 我将在图片数据中图片。 我需要从数据库中检索和显示的图片库的所有图像数据。 有没有办法做到这一点。

下面是我的code

 进口的java.util.ArrayList;
   进口android.app.Activity;
   进口android.content.Context;
   进口android.content.Intent;
   进口android.content.res.TypedArray;
   进口android.database.Cursor;
   进口android.database.sqlite.SQLiteDatabase;
   进口android.graphics.Bitmap;
   进口android.graphics.BitmapFactory;
   进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.BaseAdapter;
进口android.widget.Gallery;
进口android.widget.ImageView;

公共类task1images延伸活动{
    私人整数[] myImageIds;
     私人画廊的画廊;
     私人ImageView的imgView;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.images);
        SQLiteDatabase MYDB = this.openOrCreateDatabase(test.db的,SQLiteDatabase.OPEN_READWRITE,NULL);
        尝试{
            光标C = myDB.rawQuery(选择照片从邮件中,其中任务= 1和照片=''!,NULL);


            //从光标库项目
            ArrayList的galleryList =新的ArrayList();
            为(c.moveToFirst();! c.isAfterLast(); c.moveToNext()){
                整数加仑;
                加仑= c.getInt(c.getColumnIndex(图片));
                galleryList.add(GAL);
            }
            的System.out.println(大小:::+ galleryList.size());
            myImageIds =(整数[])galleryList.toArray(新的整数[galleryList.size());
            的System.out.println(myImageIds [0]);
            imgView =(ImageView的)findViewById(R.id.ImageView01);
            imgView.setImageResource(myImageIds [0]);

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

            gallery.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图父,视图V,INT位置,长的id){
            imgView.setImageResource(myImageIds [位置]);
            }
            });
          }
          赶上(例外五){
           Log.e(错误,错误,E);
          } 最后 {
           如果(MYDB!= NULL)
            myDB.close();
          }
    }
    公共类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将(){
            返回myImageIds.length;
        }

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

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

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

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

            返回imgView;
        }
    }

}
 

解决方案

没有必要做循环,你必须做出一个适配器并将其设置为您的画廊 的http://saigeethamn.blogspot.com/2010/05/gallery-view-android-developer-tutorial.html

编辑:

在getView()

  cursor.moveToPosition(位置);
imageView.setImageBitmap(getImageFromBLOB(cursor.getBlob(cursor.getColumnIndex(字段名))));
 

方法:从一滴得到位图

 公共位图getImageFromBLOB(byte []的mBlob){
    byte []的BB = mBlob;
    返回BitmapFactory.de codeByteArray(BB,0,bb.length);

}
 

编辑:

  @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看VI = convertView;
        ImageView的mImageView;
        cursor.moveToPosition(位置);

            如果(convertView == NULL)
            {
                LayoutInflater INF = mActivity.getLayoutInflater();
                VI = inf.inflate(R.layout.xxxxxx,NULL);

                mImageView =(ImageView的)vi.findViewById(R.id.imgview_row);
                 vi.setTag(mImageView);
            }
             其他
                 mImageView =(ImageView的)vi.getTag();

            尝试 {

                cursor.moveToPosition(位置);
                mImageView.image.setImageBitmap(AllMethod.getImageFromBLOB(cursor.getBlob(cursor.getColumnIndex("fieldname"))));

            }赶上(例外五){
                // TODO:处理异常
                e.printStackTrace();
            }
        }
        返回六;
    }
 

I am new to android development. My question is i am able to retrieve the data from sqlite database. i am converting the image data in to image. I need to retrieve all the images data from database and display as image gallery. Is there any way to do it.

Below is my code

   import java.util.ArrayList;
   import android.app.Activity;
   import android.content.Context;
   import android.content.Intent;
   import android.content.res.TypedArray;
   import android.database.Cursor;
   import android.database.sqlite.SQLiteDatabase;
   import android.graphics.Bitmap;
   import android.graphics.BitmapFactory;
   import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class task1images extends Activity  {
    private Integer[] myImageIds;
     private Gallery gallery;
     private ImageView imgView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.images);     
        SQLiteDatabase myDB = this.openOrCreateDatabase("test.db", SQLiteDatabase.OPEN_READWRITE, null);
        try{
            Cursor c = myDB.rawQuery("select photo from messages where task=1 and photo!=''"  , null);  


            //get the gallery items from the cursor
            ArrayList galleryList = new ArrayList();
            for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                Integer gal;
                gal = c.getInt(c.getColumnIndex("photo"));
                galleryList.add(gal);               
            }
            System.out.println("size:::"+galleryList.size());
            myImageIds =(Integer[])galleryList.toArray(new Integer[galleryList.size()]);
            System.out.println(myImageIds[0]);
            imgView = (ImageView)findViewById(R.id.ImageView01);
            imgView.setImageResource(myImageIds[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(myImageIds[position]);
            }
            });
          }
          catch(Exception e) {
           Log.e("Error", "Error", e);
          } finally {
           if (myDB != null)
            myDB.close();
          }
    }
    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 myImageIds.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(myImageIds[position]);
            imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);

            return imgView;
        }
    }

}

解决方案

No need to do looping, you have make an adapter and set it to your gallery http://saigeethamn.blogspot.com/2010/05/gallery-view-android-developer-tutorial.html

EDITED:

in getView()

cursor.moveToPosition(position);
imageView.setImageBitmap(getImageFromBLOB(cursor.getBlob(cursor.getColumnIndex("fieldname"))));

Method: to get Bitmap from Blob

public Bitmap getImageFromBLOB(byte[] mBlob) {
    byte[] bb = mBlob;
    return BitmapFactory.decodeByteArray(bb, 0, bb.length);

}

Edited:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ImageView mImageView;
        cursor.moveToPosition(position);

            if(convertView==null)
            {
                LayoutInflater inf=mActivity.getLayoutInflater();
                vi=inf.inflate(R.layout.xxxxxx, null);

                mImageView=(ImageView)vi.findViewById(R.id.imgview_row);
                 vi.setTag(mImageView);
            }
             else
                 mImageView=(ImageView)vi.getTag();

            try {

                cursor.moveToPosition(position); 
                mImageView.image.setImageBitmap(AllMethod.getImageFromBLOB(cursor.getBlob(cursor.getColumnIndex("fieldname"))));

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        return vi;
    }

这篇关于如何显示在Android的图片廊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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