如何插入图片在SQLite数据库? [英] How to Insert Image in Sqlite Database?

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

问题描述

在这里我想送我的形象在数据库..但我不能指定imgPro到values​​.put ..请帮助我..

 否则,如果(要求code == 2){

            乌里selectedImage = data.getData();
            的String []文件路径= {MediaStore.Images.Media.DATA};
            光标C = getContentResolver()查询(selectedImage,文件路径,NULL,NULL,NULL);
            c.moveToFirst();
            INT参数:columnIndex = c.getColumnIndex(文件路径[0]);
            字符串picturePath = c.getString(参数:columnIndex);
            c.close();
            位图的缩略图=(BitmapFactory.de codeFILE(picturePath));
            imgPro.setImageBitmap(缩略图);


        }
 

解决方案

您可以像下面。

 公共类ImageConverter {

    公共静态字符串imageToStringConverter(位图图像​​){
        ByteArrayOutputStream流=新ByteArrayOutputStream();
        image.com preSS(Bitmap.Com pressFormat.PNG,100,流);
        字节[]的字节数组= stream.toByteArray();
        字符串imageToString = Base64.en codeToString(字节数组,Base64.NO_WRAP);
        返回imageToString;
    }

    公共静态位图stringToimageConverter(字符串和imagestring){
        byte []的stringTobyte = Base64.de code(和imagestring,Base64.NO_WRAP);
        BMP位= BitmapFactory.de codeByteArray(stringTobyte,0,stringTobyte.length);
        返回BMP;
    }

}
 

您将调用这个两函数的图像转换成字符串,然后保存在下面
的database.Like

 字符串statusImage = ImageConverter.imageToStringConverter(selectedImage);
 

selectedImage 是您从文件中选择位图。如果ü想显示图像,然后只需再次将字符串转换回图像。
现在为什么Base64的??

  

当你有你想通过网络来​​运送一些二进制数据,   你通常不会只是流的比特和字节在这样做   线材以原始的格式。为什么?因为有些媒体的发   流文本。你永远不知道 - 某些协议可能跨preT你   二进制数据作为控制字符(如调制解调器),或您的二进制数据   可能会搞砸了,因为底层协议可能会认为,   您输入特殊字符组合(例如如何FTP   转换行尾)。

     

因此​​,要解决这个问题,人们EN $ C C二进制数据为字符$。   的base64是这些类型的编码中的一个。为什么64?因为你能   通常依赖于相同的64个字符是present在许多   字符集,你就有理由相信,在你的数据的   最终将会在电线两袖清风的另一端。

here I want to send my image in database.. but i Can't assign imgPro to values.put.. Please help me..

else if (requestCode == 2) {

            Uri selectedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            String picturePath = c.getString(columnIndex);
            c.close();
            Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
            imgPro.setImageBitmap(thumbnail);


        }

解决方案

You can do like below.

   public class ImageConverter {

    public static String imageToStringConverter(Bitmap image){
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        String imageToString = Base64.encodeToString(byteArray, Base64.NO_WRAP);
        return imageToString;
    }

    public static Bitmap stringToimageConverter(String imageString){
        byte[] stringTobyte = Base64.decode(imageString, Base64.NO_WRAP);
        Bitmap bmp = BitmapFactory.decodeByteArray(stringTobyte, 0, stringTobyte.length);
        return bmp;
    }

}

You will call this two function to convert the image into string and then save in the database.Like below

String statusImage = ImageConverter.imageToStringConverter(selectedImage);

selectedImage is the bitmap you have selected from file. and if u want show the image then just again convert the string back to image.
Now why Base64??

When you have some binary data that you want to ship across a network, you generally don't do it by just streaming the bits and bytes over the wire in a raw format. Why? because some media are made for streaming text. You never know -- some protocols may interpret your binary data as control characters (like a modem), or your binary data could be screwed up because the underlying protocol might think that you've entered a special character combination (like how FTP translates line endings).

So to get around this, people encode the binary data into characters. Base64 is one of these types of encodings. Why 64? Because you can generally rely on the same 64 characters being present in many character sets, and you can be reasonably confident that your data's going to end up on the other side of the wire uncorrupted.

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

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