[英]Problem on inserting & retrieve image data in sqlite database using android
I am new to android development. 我是android开发的新手。 Currently i am facing a problem while inserting/retrieving
image to SQLite database
using android. 目前,我在使用Android inserting/retrieving
图像inserting/retrieving
到SQLite 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);
But no image is shown.Log shows "Factory returned null" 但是未显示任何图像。日志显示“工厂返回null”
Can anyone help me to find out where I am wrong and provide me code snippet of a working solution? 谁能帮助我找出我的错误地方,并提供有效解决方案的代码段?
Thanks in advance 提前致谢
You may use decodeByteArray: 您可以使用decodeByteArray:
final byte[] image_byte = c.getBlob(0);
Bitmap image_bitmap = BitmapFactory.decodeByteArray(image_byte, 0, image_byte.length);
and possibly Bitmap.createScaledBitmap() to rescale it if needed. 如果需要,还可以使用Bitmap.createScaledBitmap()重新缩放它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.