亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在SQLite數據庫中存儲圖像

如何在SQLite數據庫中存儲圖像

如何在SQLite數據庫中存儲圖像在我的應用程序中,我從圖片庫上傳一個圖像,并希望將該圖像存儲在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;     }    });   }    }   }  }
查看完整描述

3 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

你必須使用“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;}


查看完整回答
反對 回復 2019-06-20
  • 3 回答
  • 0 關注
  • 2074 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號