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