Remmyabhavan
Remmyabhavan

Reputation: 1719

BitmapFactory.decodeByteArray returns null in android

In my application I am converting base64 string to image.For that I initially converted base 64 file to byte array and later am trying to convert to images. To convert to Images I am using the below code

    File sdImageMainDirectory = new File("/data/data/com.ayansys.Base64trial");
    FileOutputStream fileOutputStream = null;
    String nameFile="Images";
    try {

        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize = 5;
        options.inDither = true; 
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);


        fileOutputStream = new FileOutputStream(
                sdImageMainDirectory.toString() +"/" + nameFile + ".jpg");


        BufferedOutputStream bos = new BufferedOutputStream(
                fileOutputStream);

        myImage.compress(CompressFormat.JPEG, quality, bos);

        bos.flush();
        bos.close();

but am getting my image as null at

Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);

Please let me know your valuable suggestions.

Thanks in advance :)

Upvotes: 4

Views: 6698

Answers (2)

Phransys
Phransys

Reputation: 146

I ran into this same problem. I solved it by removing this part of the string:

"data:image/jpeg;base64,"

Upvotes: 1

the100rabh
the100rabh

Reputation: 4147

decodeByteArray will not convert base 64 encoding to byte array. You need to use Base64 to byte array converter first

Upvotes: 2

Related Questions