2010-10-21 54 views
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> 

但是,没有图像出现在屏幕上。 问题在哪里?

回答

0

可能是一些东西。这样做:

  1. 检查日志中的错误。
  2. 检查您是否从数据库中获得了相同的数据。检查大小。使用insertOrThrow()而不是insert()。
  3. 检查BitmapFactory.decodeStream(..)是否不返回空值。
+0

问题是,BitmapFactory.decodeStream(..)返回null。 – carnauser 2010-10-25 14:55:02

+0

所以数据流有问题。尝试从数据库中读取数据,然后将其写入文件并在计算机上打开。 – 2010-10-25 15:31:32

相关问题