如何从sqlite数据库中读取字节数组并转换为bmp并存储在int数组

时间:2016-04-24 19:47:16

标签: java android arrays sqlite android-viewpager

  1. 我希望从sqlite数据库中读取。数据库保存图像中的字节数组信息(blob类型)。

    1. 将字节数组转换为bmp。
    2. 将bmp存储在一个int数组中。
    3. 故事:当图像在图像视图中发生变化时,我已经从图像视图将图像输入到数据库中,我可以将其保存到sqlite数据库中。我把它转换成一个字节数组,然后按下一个按钮。下面转换代码。 Bmp指的是位图

      Bitmap myBit =((BitmapDrawable)myImageView.getDrawable())。getBitmap();                 ByteArrayOutputStream myBos = new ByteArrayOutputStream();                 myBit.compress(Bitmap.CompressFormat.PNG,100,myBos);                 byte [] img = myBos.toByteArray();

      现在我想查看我存储在该数据库中的所有图像。 我使用了一个PagerAdapter来查看已经在我的drawable文件夹中的项目,如下所示

      private int[] image_resources = {R.drawable.community_btn_txtpn,R.drawable.logo_design,R.drawable.photo_camerabtn_txt,
      R.drawable.plant_for_gallery,R.drawable.save_for_sharing_txt,R.drawable.seed_savebtn_txt,R.drawable.seed_sharebtn_txt,
      R.drawable.seeds_for_sharing_txt};
      

      我需要那个数组来保存我的数据库中的图像。

      和数据库查询中的代码从数据库中读取。我已经走到了这一步

          public Cursor getImage(){
              SQLiteDatabase db = this.getReadableDatabase();
              Cursor C = db.rawQuery("select image from " + TABLE_IMAGE,null);
      
              return C;
          }
      
        来自PagerAdapter的代码

       package com.jonnyg.gardenapp;
      
      import android.content.Context;
      import android.database.Cursor;
      import android.database.sqlite.SQLiteDatabase;
      import android.support.v4.view.PagerAdapter;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.ImageView;
      import android.widget.LinearLayout;
      import android.widget.RelativeLayout;
      import android.widget.TextView;
      
      /**
       * Created by reiko_000 on 24/04/2016.
       */
      public class CustomSwipeAdapter extends PagerAdapter {
      
      
      
      
      
          private int[] image_resources = {R.drawable.community_btn_txtpn,R.drawable.logo_design,R.drawable.photo_camerabtn_txt,
          R.drawable.plant_for_gallery,R.drawable.save_for_sharing_txt,R.drawable.seed_savebtn_txt,R.drawable.seed_sharebtn_txt,
          R.drawable.seeds_for_sharing_txt};
      
      
      
          private Context ctx;
          private LayoutInflater layoutInflater;
      
          public CustomSwipeAdapter(Context ctx){
              this.ctx = ctx;
      
          }
          @Override
          public int getCount() {
              return image_resources.length;
          }
      
          @Override
          public boolean isViewFromObject(View view, Object object) {
              return (view ==(RelativeLayout)object);
          }
      
          @Override
          public Object instantiateItem(ViewGroup container, int position) {
              layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
              ImageView imageView = (ImageView)item_view.findViewById(R.id.image_view_gallery);
              TextView textView = (TextView)item_view.findViewById(R.id.image_count);
              TextView photoSlide = (TextView)item_view.findViewById(R.id.txtVPhotoSlide);
              imageView.setImageResource(image_resources[position]);
              textView.setText("Image : " + position);
              container.addView(item_view);
      
      
      
      
      
              return item_view;
          }
      
          @Override
          public void destroyItem(ViewGroup container, int position, Object object) {
             //work faster
              container.removeView((RelativeLayout)object);
          }
      }
      

0 个答案:

没有答案