Android数据库存储&检索

时间:2015-09-16 01:32:02

标签: android database

您好,我是Android新手。有人可以描述将图像存储到Android数据库和放大器的方式吗?检索它?

1 个答案:

答案 0 :(得分:0)

看看这个教程......

http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html

您可以使用此代码:
用于存储图像:

Map<String, byte[]> hh = new HashMap<String, byte[]>();

String hi = "http://i.stack.imgur.com/TLjuP.jpg";
byte[] logoImagedata = getLogoImage(hi);       

hh.put("img",logoImagedata);

用于复原:

byte[] imageByteArray = hh.get("img");

ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage= BitmapFactory.decodeStream(imageStream);

和getLogoImage:

private byte[] getLogoImage(String url){
       try {
               URL imageUrl = new URL(url);
               URLConnection ucon = imageUrl.openConnection();

               InputStream is = ucon.getInputStream();
               BufferedInputStream bis = new BufferedInputStream(is);

               ByteArrayBuffer baf = new ByteArrayBuffer(500);
               int current = 0;
               while ((current = bis.read()) != -1) {
                       baf.append((byte) current);
               }

               return baf.toByteArray();
       } catch (Exception e) {
               Log.d("ImageManager", "Error: " + e.toString());
               return null;
       }

  }

我希望这对你有所帮助。