从SQLite加载图像swiper的源代码

时间:2015-09-23 09:27:07

标签: android sqlite

我制作了一本由140张图片组成的书,每张图片大约40kB。由于安全性不足,我不想把它们放在Drawables Folder中。所以我应该在sqlite中加载图片,并使用MikeOrtiz_TouchImageView“ViewPagerExample”类进行滑动和缩放页面。

我的问题是如何将图像从数据库加载到图像swiper数组:int[] images或任何数组(我是初学者!请详细说明)

package com.ortiz.touch;

import com.example.touch.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class ViewPagerExampleActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager_example);
        ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
        mViewPager.setAdapter(new TouchImageAdapter());
    }

    static class TouchImageAdapter extends PagerAdapter {

        private static int[] images = {
            R.drawable.nature_1, R.drawable.nature_2, R.drawable.nature_3, R.drawable.nature_4, R.drawable.nature_5
        };

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            TouchImageView img = new TouchImageView(container.getContext());
            img.setImageResource(images[position]);
            container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            return img;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }
}

除了那些解释之外,还有其他建议吗?

2 个答案:

答案 0 :(得分:0)

保存到数据库:        bitmap to byte []; 用户图片:        byte []到位图; 但它无用; 如果他们愿意,人们可以获得你的来源;

答案 1 :(得分:0)

您可以将数据库中的图像作为Blob获取,并可以转换为位图数组

  String query = "SELECT * FROM '" + tablename+ "'";
      Cursor tileCursor = database.rawQuery(query,null);//create a database object
    ArrayList<Bitmap> images=new ArrayList<Bitmap>();

    while(!tileCursor.isAfterLast())
    {
    byte[] bitmapdata =tileCursor.getBlob(tileCursor.getColumnIndex("Imagecolumnname"));
    Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
     images.Add(bitmap );
  tileCursor.moveToNext();
    }

然后使用

进行设置
  @Override
    public View instantiateItem(ViewGroup container, int position) {
        TouchImageView img = new TouchImageView(container.getContext());
        img.setImageBitmap(images.get(position));
        container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        return img;
    }