TylerM
TylerM

Reputation: 151

base64.java image to string

I am trying to use the base64.java to convert and image into a string using

String image_str = Base64.encodeToString(bitmap, Base64.DEFAULT);

The problem is it underlines .DEFAULT saying DEFAULT cannot be resolved or is not a field. Now I see in every example I found this is what they use, so why isn't it working when I try it?

the whole function that it is in looks like

private void previewCapturedImage() {
    try {
        // hide video preview

        imgPreview.setVisibility(View.VISIBLE);

        // bimatp factory
        BitmapFactory.Options options = new BitmapFactory.Options();

        // downsizing image as it throws OutOfMemory Exception for larger
        // images
        options.inSampleSize = 8;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                options);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
        byte[] b = baos.toByteArray(); 
        String image_str = Base64.encodeToString(b, Base64.DEFAULT);        
        imgPreview.setImageBitmap(bitmap);

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

Thank you, Tyler

Upvotes: 0

Views: 1738

Answers (4)

GrouchyPanda
GrouchyPanda

Reputation: 1084

Your code looks just like mine where I have that implemented. Only thing I can think to check is make sure you are importing android.util.Base64, and these APIs were added in API 8, so if your target is below that then you will have problems.

Upvotes: 1

user3215598
user3215598

Reputation: 103

Import android.util.base64 instead of the org.apache.commons.codec.binary.Base64?

Upvotes: 2

gerrytan
gerrytan

Reputation: 41103

Are you using the correct Base64 class? Based on you code sample it should be android.util.Base64 (check what you got in your imports).

Upvotes: 1

nimsson
nimsson

Reputation: 950

try using the com.sun.org.apache.xerces.internal.impl.dv.util.Base64 class with only two methods String encode(byte[] binary) and byte[] decode(String s)

This may a) not be for android and b) be removed in a future release of Java.

Upvotes: 1

Related Questions