如何在SQLite数据库中存储图像?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (301)

在我的应用程序中,我从图片库上传一个图像,并希望将该图像存储在SQLite数据库中。如何在数据库中存储位图?我正在将位图转换为字符串,并将其保存在数据库中。当从数据库检索它时,我无法将该字符串分配给ImageView,因为它是一个字符串。

Imageupload 12.java:

 public class Imageupload12 extends Activity {
  Button buttonLoadImage;
  ImageView targetImage;
  int i = 0;
  Database database = new Database(this);
  String i1;
  String img;
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main5);
   buttonLoadImage = (Button) findViewById(R.id.loadimage);
   targetImage = (ImageView) findViewById(R.id.targetimage);


   Bundle b = getIntent().getExtras();
   if (b != null) {
    img = b.getString("image");
    targetImage2.setImageURI("image");
    //i am getting error as i cant assign string to imageview.

   }

   buttonLoadImage.setOnClickListener(new Button.OnClickListener() {

    public void onClick(View arg0) {
     // TODO Auto-generated method stub
     Intent intent = new Intent(Intent.ACTION_PICK,
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     Log.i("photo", "" + intent);
     startActivityForResult(intent, i);
     i = i + 1;
    }
   });

  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

   // TODO Auto-generated method stub
   super.onActivityResult(requestCode, resultCode, data);
   switch (requestCode) {

    case 0:
     if (resultCode == RESULT_OK) {
      Uri targetUri = data.getData();
      //             textTargetUri.setText(targetUri.toString());
      Bitmap bitmap;
      try {
       bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
       targetImage.setImageBitmap(bitmap);

       i1 = bitmap.toString();
       Log.i("firstimage........", "" + i1);
       targetImage.setVisibility(0);

       SQLiteDatabase db = database.getWritableDatabase();
       db.execSQL("INSERT INTO UPLOAD VALUES('" + i1 + "');");

      } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
     }
     break;



   }

  }
 }

Image.class:

public class Image extends Activity {
 Database database = new Database(this);
 static EfficientAdapter adapter, adapter1;
 static ListView lv1;

 static SQLiteDatabase db;
 static EfficientAdapter adp;
 static Cursor c1;

 static Vector < String > IMAGE = new Vector < String > ();

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  db = database.getReadableDatabase();
  c1 = db.rawQuery("select * from UPLOAD;", null);

  if (c1.moveToFirst()) {

   do {
    IMAGE.add(c1.getString(0).toString());

   } while (c1.moveToNext());

   c1.close();
  }

  lv1 = (ListView) findViewById(R.id.List);

  adapter = new EfficientAdapter(this);


  lv1.setAdapter(adapter);

  ImageView add = (ImageView) findViewById(R.id.imv1a);



  add.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    IMAGE.clear();

    Intent i = new Intent(Image.this, Imageupload12.class);
    startActivity(i);


   }
  });


 }



 private static class EfficientAdapter extends BaseAdapter {


  //        protected  final Context Context = null;
  protected LayoutInflater mLayoutInflater;
  AlertDialog.Builder aBuilder;
  public EfficientAdapter(Context context) {
   // TODO Auto-generated constructor stub
   mLayoutInflater = LayoutInflater.from(context);
  }

  @Override
  public int getCount() {
   // TODO Auto-generated method stub

   return IMAGE.size();
  }

  @Override
  public Object getItem(int position) {
   // TODO Auto-generated method stub
   return position;
  }

  @Override
  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return position;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub

   final ViewHolder mVHolder;
   if (convertView == null) {
    convertView = mLayoutInflater.inflate(R.layout.pjtlistdetails, parent, false);

    mVHolder = new ViewHolder();

    mVHolder.t1 = (TextView) convertView.findViewById(R.id.pjtdetails);
    mVHolder.time = (TextView) convertView.findViewById(R.id.name);


    mVHolder.imv = (ImageButton) convertView.findViewById(R.id.editic);
    mVHolder.imvd = (ImageView) convertView.findViewById(R.id.delete);
    mVHolder.imvf = (ImageView) convertView.findViewById(R.id.fwd);





    mVHolder.imv.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {



      String img = IMAGE.elementAt(position);
      Log.i("image...", "" + img);

      Context ctx = v.getContext();
      Intent myIntent = new Intent();
      ctx = v.getContext();
      myIntent.setClass(ctx, Imageupload12.class);
      myIntent.putExtra("image", img);

      ctx.startActivity(myIntent);

      IMAGE.clear();

     }
    });
    static class ViewHolder {

     ImageButton imv;
     ImageView imvd, imvf;
    }
   }
  }
 }
}
提问于
用户回答回答于

你必须使用“BLOB”来存储图像。

将图像存储到db中

public void insertImg(int id , Bitmap img ) {   


    byte[] data = getBitmapAsByteArray(img); // this is a function

    insertStatement_logo.bindLong(1, id);       
    insertStatement_logo.bindBlob(2, data);

    insertStatement_logo.executeInsert();
    insertStatement_logo.clearBindings() ;

}

 public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, outputStream);       
    return outputStream.toByteArray();
}

从db检索图像

public Bitmap getImage(int i){

    String qu = "select img  from table where feedid=" + i ;
    Cursor cur = db.rawQuery(qu, null);

    if (cur.moveToFirst()){
        byte[] imgByte = cur.getBlob(0);
        cur.close();
        return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
    }
    if (cur != null && !cur.isClosed()) {
        cur.close();
    }       

    return null ;
} 
用户回答回答于

使用BLOB将图像存储在sqite数据库中。下面是如何使用BLOB的示例。

建立数据库

CREATE TABLE " + DB_TABLE + "("+ 
                   KEY_NAME + " TEXT," + 
                   KEY_IMAGE + " BLOB);";

在数据库中插入:

public void addEntry( String name, byte[] image) throws SQLiteException{
    ContentValues cv = new  ContentValues();
    cv.put(KEY_NAME,    name);
    cv.put(KEY_IMAGE,   image);
    database.insert( DB_TABLE, null, cv );
}

检索数据:

 byte[] image = cursor.getBlob(1);
  1. 在插入数据库之前,您需要先将位图图像转换为字节数组,然后使用数据库查询应用它。
  2. 当从数据库检索时,当然有一个字节数组的图像,您需要做的是将字节数组转换回原始图像。所以,您必须使用BitmapFactory来解码。
public class DbBitmapUtility {

    // convert from bitmap to byte array
    public static byte[] getBytes(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, stream);
        return stream.toByteArray();
    }

    // convert from byte array to bitmap
    public static Bitmap getImage(byte[] image) {
        return BitmapFactory.decodeByteArray(image, 0, image.length);
    }
}

扫码关注云+社区

领取腾讯云代金券