插入问题&使用android在sqlite数据库中检索图像数据

时间:2010-10-27 15:05:29

标签: android sqlite

我是Android开发的新手。目前我正在使用android。{/ 1> inserting/retrievingSQLite database映像设置为//code for inserting image InputStream in = OpenHttpConnection(imgurl); byte[] bb = new byte[in.available()]; in.read(bb);//here the length of bb is 2040 //update query myDataBase.execSQL("Update Product SET Image='"+bb+"' Where CatPID='"+pcpid+"'"); //function OpenHttpConnection private InputStream OpenHttpConnection(String urlString) throws IOException { InputStream in = null; int response = -1; URL url = new URL(urlString); URLConnection conn = url.openConnection(); if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection"); try{ HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); response = httpConn.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { in = httpConn.getInputStream(); } } catch (Exception ex) { throw new IOException("Error connecting"); } return in; } //code for retrieve image from database byte[] bb = cursor.getBlob(cursor.getColumnIndex("Image"));//here length is only 12 ByteArrayInputStream imageStream = new ByteArrayInputStream(bb); Bitmap theImage = BitmapFactory.decodeStream(imageStream); img.setImageBitmap(theImage); 时遇到问题

{{1}}

但是没有显示图像。日志显示“工厂返回null”

任何人都可以帮我找出错误的地方,并提供工作解决方案的代码片段吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用decodeByteArray:

final byte[] image_byte = c.getBlob(0);
Bitmap image_bitmap = BitmapFactory.decodeByteArray(image_byte, 0, image_byte.length);

并且可能Bitmap.createScaledBitmap()在需要时重新缩放它。