如何在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個贊
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();}
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;}
添加回答
舉報
0/150
提交
取消