0
 public static void writeBitmapWithCompress(final String localFileName, Bitmap b){
        int bytes = b.getByteCount();

        ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
        b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
        byte[] array = buffer.array();
       // byte[] encodedString  = Base64.encode(array, Base64.DEFAULT);
        byte[] decodedString = Base64.decode(array, Base64.DEFAULT);
       // Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

        Timber.d(">> social localFileName"+ localFileName);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(localFileName);
            fileOutputStream.write(decodedString);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

getting exception at this line --> byte[] decodedString = Base64.decode(array, Base64.DEFAULT); bad base64. Is other way to do this task without using any compression ?

stacktrace--- >

at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 02-08 at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635) I/dalvikvm:
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417)
at nl.changer.socialschools.common.Utils.resizeImage(Utils.java:408)
at nl.changer.socialschools.common.Utils.uploadPhotos(Utils.java:321) at nl.changer.socialschools.AsyncPostService.createPost(AsyncPostService.java:75) nl.changer.socialschools.AsyncPostService.onHandleIntent(AsyncPostService.java:50) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:110) 02-08 android.os.Looper.loop(Looper.java:193) 02-08 11:41:36.214 android.os.HandlerThread.run(HandlerThread.java:61)

4
  • show the full exception stacktrace and message. It explains the problem usually. Commented Feb 8, 2017 at 6:09
  • You should encode the bytes to a base64 string. Not decode as you try to do now. Commented Feb 8, 2017 at 8:04
  • But why do you want to use base64? You can write the byte buffer directly to file. Commented Feb 8, 2017 at 8:05
  • If I don't decode then I was unable to see image in gallery. Do you have any other alternative? Commented Feb 8, 2017 at 8:40

1 Answer 1

1

Try this,

 public void writeBitmapWithCompress(final String localFileName, Bitmap b){

    byte[] image_bytes = getBytes(b);

    try {
        FileOutputStream out = new FileOutputStream(localFileName);
        out.write(image_bytes);
        out.close();

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


// convert bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}
Sign up to request clarification or add additional context in comments.

1 Comment

don't want to use compression. Want to write file directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.