如何使用android在mysql中存储图片

fxnxkyjh  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(243)

我试图开发一个android应用程序,允许用户从他的图库中添加图片,它工作正常,并在图像视图中显示图片。问题是我试图保存在mysql数据库(不是url或路径)这张照片,我想存储为一个blob。我尝试了下面的代码从用户的图库中获取图像,并将其显示在图像视图中。属性test2是一个字符串,它是我保存在数据库中的内容

if (requestCode == GET_FROM_GALLERY && resultCode == Activity.RESULT_OK && data != null) {
    Uri selectedImageUri = data.getData();
    imagepath = getPath(selectedImageUri);
}
////// edit
Bitmap image = BitmapFactory.decodeFile(imagepath);
FinalBytes = getBytes(image); // this will be save in DB
Bitmap getIt = getBitmap(FinalBytes);
imgV.setImageBitmap(getIt);
imgV.setDrawingCacheEnabled(true);
imgV.buildDrawingCache();
Bitmap testbit = imgV.getDrawingCache();
ByteArrayOutputStream testbyte = new ByteArrayOutputStream();
testbit.compress(Bitmap.CompressFormat.JPEG, 100, testbyte);
testbyte2 = testbyte.toByteArray();
base64Image = Base64.encodeToString(testbyte2, Base64.DEFAULT);

我使用下面的代码检索图像

byte[] decodedString = Base64.decode(Recipes[position].getRimage(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.image.setImageBitmap(decodedByte);

但我在解码时得到了这个信息。

illegalargumentexception bad base 64

请帮助我花了2天时间处理这个错误

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题