如果blob超过2MB,则getBlob()崩溃,substr方法不起作用

时间:2015-05-14 22:50:29

标签: java android cursor

我正在尝试使用getBlob方法将图片检索到byte []。

但是,如果blob超过2MB,我发现它会崩溃。

我发现一篇文章声明要使用substr函数将其分段,但这不起作用。

继承我的样本,它非常粗糙。

                      if( size > 2000000 ) {
                        c = db.rawQuery("SELECT substr(PICTURE, 1, 1000000)  FROM PICS WHERE ID = 1, null);
                        if( c.moveToFirst() ) {
                            byte[] image1, image2, image3, image4, image5;
                            image2 = c.getBlob(0);
                            c = db.rawQuery("SELECT substr(PICTURE, 1000001, 2000000)  FROM PICS WHERE ID = 1, null);
                            if( c.moveToFirst() ) {
                                image3 = c.getBlob(0);
                                image4 = concatenateByteArrays(image2, image3);
                                c = db.rawQuery("SELECT substr(PICTURE, 2000001, 3000000) FROM PICS WHERE ID = 1, null);
                                if( c.moveToFirst() ) {
                                    image5 = c.getBlob(0);
                                    image1 = concatenateByteArrays(image4, image5);
                                    ByteArrayInputStream inputStream = new ByteArrayInputStream(image1);
                                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                    image.setImageBitmap(bitmap);
                                }
                            }
                        }

任何有关这方面的帮助都会被贬低。

感谢。

1 个答案:

答案 0 :(得分:1)

这是一个很好的例子,说明如何从blob列中读取数据

的Class.forName( “YOUR_DB_DRIER”);     Connection conn = DriverManager.getConnection(url,username,password);

String sql = "SELECT name, description, image FROM pictures ";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
  String name = resultSet.getString(1);
  String description = resultSet.getString(2);
  File image = new File("D:\\java.gif");
  FileOutputStream fos = new FileOutputStream(image);

  byte[] buffer = new byte[512]; // as much as you increase this array  
             //   it will increase the performance of you data reading. 
  InputStream is = resultSet.getBinaryStream(3);
  while (is.read(buffer) > 0) {
    fos.write(buffer);
  }
  fos.close();
}