简体   繁体   English

Android:将图像从相机存储到数据库并显示

[英]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: 做这个:

  1. Check the log for ERRORs. 检查日志中的错误。
  2. Check that you get the same data from database that you put in. Check size. 检查是否从放入的数据库中获得了相同的数据。检查大小。 Use insertOrThrow() instead of insert(). 使用insertOrThrow()而不是insert()。
  3. Check that BitmapFactory.decodeStream(..) does not return null. 检查BitmapFactory.decodeStream(..)是否不返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM