[英]Android : Store an image from camera to database and the show it
Here I write camera snapshot on the db. 在这里,我将数据库快照写在数据库上。
`private SQLiteDatabase db;
// SOME CODE HERE ///
InputStream is =new ByteArrayInputStream(data);
BufferedInputStream bis = new BufferedInputStream(is,128);
ByteArrayBuffer baf = new ByteArrayBuffer(128);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
baf.toByteArray();
ContentValues initialValues = new ContentValues();
initialValues.put("mapimg", baf.toByteArray();
db.insert(DATABASE_TABLE, null, initialValues);
` This writes correctly in the db a new row. `这样可以在db中正确写入新行。
When I try to show the image : 当我尝试显示图像时:
DbUtil db = new DbUtil(this);
List<Row> rows = db.fetchAllRows();
db.close();
byte[] imageByteArray= rows.get(10).getMapimg();
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
ImageView imageView ;
imageView = (ImageView)findViewById(R.id.ImageView01);
imageView.setImageBitmap(theImage);
In the xml : 在xml中:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/ImageView01"
android:layout_width="300px"
android:layout_height="300px"
android:scaleType="center"
/>
</LinearLayout>
But no image appears on screen. 但是屏幕上没有图像。 Where is the problem ? 问题出在哪里 ?
Could be number of things. 可能是很多事情。 Do this: 做这个:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.