如何将图像保存在数据库中并重新加载以在图像视图中查看?
不要保存目录以显示图像,将文件(图像)移动到数据库

Android版本2.2

最佳答案

试试这个保存图像:

private void saveDownloadedImage(Bitmap bmp, String id) {
    if (bmp != null) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] imgBytes = baos.toByteArray();
        String base64String = Base64.encodeToString(imgBytes,
                Base64.DEFAULT);

        ContentValues initialValues = new ContentValues();

        initialValues.put("picture", base64String);

        // save your base64String to DB
    }
}


这对于设置图像:

private Bitmap setImage(String base64String) {
    Bitmap bmp = null;
    try {
        if (base64String == null || base64String.equals("")) {


        } else {

            byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT);
            bmp =  BitmapFactory.decodeByteArray(
                    decodedString, 0, decodedString.length);

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bmp;
}

09-25 16:06