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

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

问题描述

我想知道是否有办法将图像(.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:

You should use BLOB in your database:

查看本教程...

但我认为你应该下载并存储在HashMap中的图像,这样会更简单.

But I think you should download and store image in HashMap, which will make it simpler.

代码:

训练

var imageMap = new HashMap<String, byte[]>();

var imageUrl = "http://i.stack.imgur.com/TLjuP.jpg";

var imagedata = GetImage(imageUrl);      

imageMap.put("img",imagedata);


检索

var imageData = imageMap.get("img");

var imageStream = new ByteArrayInputStream(imageData);

var image = BitmapFactory.decodeStream(imageStream);


获取图像

private byte[] GetImage(String url)
{
    try
    {
        var imageUrl = new URL(url);
        var urlConnection = imageUrl.openConnection();

        var inputStream = urlConnection.getInputStream();
        var bufferedInputStream = new BufferedInputStream(inputStream);

        var byteArrayBuffer = new ByteArrayBuffer(500);

        int current = 0;
        while ((current = bis.read()) != -1)
        {
            byteArrayBuffer.append((byte)current);
        }

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

希望对你有帮助.

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

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