我刚刚从localhost数据库上传和检索图像到android。我怎样才能做到这一点 ?我已经尝试了三(3)天,我不知道如何编码。事情就是这样:
答案 0 :(得分:0)
首先上传之前将图像更改为字节数组
ByteArrayOutputstream bos;
Bitmap bm ;
byte[] bitmapdata,bb;
bm= BitmapFactory.decodeResourses(getResources() ,R.drawable.image);
bos = new ByteArrayOutputStream();
String convert_txt;
当您在数据库中以字节格式存储图像时,尝试从数据库中获取图像
bitmapdata = bos.tobyteArray();
for(int i =0 i<bitmapdata.length ; i++){
convert_txt = convert_txt+bitmapdata[i];
}
Cursor cursor = dh.rawQuery("Select statement....");
bb = cursor.getblob(cursor.getColumnIndex("columnanme"));
答案 1 :(得分:0)
以下是将图像编码和解码为Base64
您需要传递位图的简单函数,它将返回一个字符串:
/**
* @param bitmap
* @return converting bitmap and return a string
*/
public String BitMapToString(Bitmap bitmap){
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
return temp;
}
以下是将字符串转换为位图的相反过程,但字符串应为Base64编码
/**
* @param encodedString
* @return bitmap (from given string)
*/
public Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
所以现在您可以将已编码的Image String保存到数据库中,就像您已经为其他变量所做的那样,将String解码为Bitmap
答案 2 :(得分:0)
我的代码在这里,因为它是关于Profile Image的,我也在使用Android模拟器,其中我有来自SDCARD的图像并且它已成功更改但它不会从数据库中保存。我只是我的一些代码。
imageProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImageFromGallery();
}
});
public void selectImageFromGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Toast.makeText(this.getApplicationContext(), picturePath, Toast.LENGTH_LONG).show();
decodeFile(picturePath);
}
}
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 270;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeFile(filePath, o2);
imageProfile.setImageBitmap(bitmap);
}