简体   繁体   中英

Bitmap for an ImageView is null in Android ( MySQL DataBase)

I am saving byteArray of an image in MySQL Database, and Retrieving from Databses then coverting string to byteArray then byteArray to get Bitmap. But Bitmap is Null i have tried a lot of codes but still NULL. Saving an Image

private String imageviewtobyte(ImageView view){
        Bitmap bitmap=((BitmapDrawable) view.getDrawable()).getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byteArray = stream.toByteArray();
        ConvertImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
        return ConvertImage;

    }

Getting an Image

 imgData=result;
    byte[] byteArray = Base64.decode(result, Base64.DEFAULT);
    Bitmap bMap = null;
    bMap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
    testimg.setImageBitmap(bMap);

Looking for experts to getting exact error with solution.

//check result object it is null or not
if(result != null){
     imgData = result;
     Bitmap bitmap = StringToBitMap(imgData);
     if(bitmap != null){
         testimg.setImageBitmap(bitmap);
     }
}   

/**
 * @param encodedString
 * @return bitmap (from given string)
 */
public static 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;
    }
}

Base64String is not working well for the large bitmap. So first you have to decrease the size of a bitmap before converting it to the base64String. I attached a code for decreasing the size of the bitmap. You can use maxWidth = 960.0 and maxheight = 1280.0 in this code.

    public Bitmap GetBitmap(Bitmap finalimage) {
    int actualHeight = finalimage.getHeight();
    int actualWidth = finalimage.getWidth();
    float imgRatio = actualWidth / actualHeight;
    float maxRatio = maxWidth / maxHeight;

    if (actualHeight > maxHeight || actualWidth > maxWidth) {
        if (imgRatio < maxRatio) {
            imgRatio = maxHeight / actualHeight;
            actualWidth = (int) (imgRatio * actualWidth);
            actualHeight = (int) maxHeight;
        } else if (imgRatio > maxRatio) {
            imgRatio = maxWidth / actualWidth;
            actualHeight = (int) (imgRatio * actualHeight);
            actualWidth = (int) maxWidth;
        } else {
            actualHeight = (int) maxHeight;
            actualWidth = (int) maxWidth;
        }
    }
    finalimage = Bitmap.createScaledBitmap(finalimage, actualWidth, actualHeight, false);
    return finalimage;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM