如何将图像保存到数据库 [英] How to save images into Database

查看:153
本文介绍了如何将图像保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法的类型(.gif注意)图像保存到sqllite数据库。如果是的话应该怎么我的 DatabaseAdapter 的样子。

I'd like to know if there's a way to save Images (of the type .gif) to the sqllite-database. If yes how should my DatabaseAdapter look like.

也有性能问题?

推荐答案

您应该使用 BLOB 在数据库:

阅读本教程...

<一个href="http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html">http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html

但一想到我告诉你降负荷和存储图像的地图,这是适合你简单。

but one think i tell you down load and store image in map this is suits you simple.

code:

Stroring -

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); 

retriving -

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;
           }

      }

这会帮助你。

这篇关于如何将图像保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆