0
在这里,我在数据库上写入相机快照。Android:将图像从相机存储到数据库并显示它
`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);
` 这会在db中正确写入新行。
当我尝试显示图像:
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);
中的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>
但是,没有图像出现在屏幕上。 问题在哪里?
问题是,BitmapFactory.decodeStream(..)返回null。 – carnauser 2010-10-25 14:55:02
所以数据流有问题。尝试从数据库中读取数据,然后将其写入文件并在计算机上打开。 – 2010-10-25 15:31:32