开发者

Android : Store an image from camera to database and the show it

开发者 https://www.devze.com 2023-01-21 00:23 出处:网络
Here I write camera snapshot on the db. `private SQLiteDatabase db; // SOME CODE HERE /// InputStream is =new ByteArrayInputStream(data);

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.

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.de开发者_C百科codeStream(imageStream);
ImageView imageView ;
imageView = (ImageView)findViewById(R.id.ImageView01);
imageView.setImageBitmap(theImage);

In the 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().
  3. Check that BitmapFactory.decodeStream(..) does not return null.
0

精彩评论

暂无评论...
验证码 换一张
取 消