Java getBlob()崩溃如果blob超过2MB,substr方法将不起作用
我正在尝试使用getBlob方法将图片检索到字节[] 但是,如果blob超过2MB,我注意到它会崩溃 我发现一篇文章说要使用substr函数将其分成多个部分,但这不起作用 这是我的样品,很粗糙Java getBlob()崩溃如果blob超过2MB,substr方法将不起作用,java,android,cursor,Java,Android,Cursor,我正在尝试使用getBlob方法将图片检索到字节[] 但是,如果blob超过2MB,我注意到它会崩溃 我发现一篇文章说要使用substr函数将其分成多个部分,但这不起作用 这是我的样品,很粗糙 if( size > 2000000 ) { c = db.rawQuery("SELECT substr(PICTURE, 1, 1000000) FROM PICS WHERE ID = 1, null
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);
}
}
}
任何有关这方面的帮助都将被告知
谢谢。这是一个很好的示例,演示了如何从blob列读取数据 Class.forName(“你的爸爸”); Connection conn=DriverManager.getConnection(url、用户名、密码)
老实说,我不建议将此类数据放入数据库。不会被关联查询的大量二进制数据不是数据库的强项。我把它放在文件系统上,以及数据库中文件的路径上。我希望它是这样的,所以它是完全自包含的。我不想读取2GB的数据,如果数据丢失,肯定不敏感。
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();
}