我只是从localhost数据库向android上传和检索图像的新手。我该怎么做?我已经想了三(3)天了,但我不知道如何编码
- 如何从android模拟器-->localhost数据库保存图像
- 如何从localhost数据库-->安卓模拟器获取图像?因此,每当我打开我的android应用程序时,它都会显示我上传的图像(只有1张图像)
上传前首先将图像更改为byteArray
ByteArrayOutputstream bos;
Bitmap bm ;
byte[] bitmapdata,bb;
bm= BitmapFactory.decodeResourses(getResources() ,R.drawable.image);
bos = new ByteArrayOutputStream();
String convert_txt;
当图像存储在数据库中的字节fromat中时,请尝试从数据库中获取图像
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"));
以下是将图像编码和解码为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;
}
}
因此,现在您可以像对其他变量所做的那样,将编码的图像字符串保存到数据库中,并将字符串解码为位图
我在这里有我的代码,因为它是关于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);
}