如何将图像转换为base64字符串

时间:2022-12-02 21:24:24

I want to convert image to base 64 encode to string. from that to send to server with oma_status-icon xml format.

我想将图像转换为base 64 encode to string。从那里以oma_status-icon xml格式发送到服务器。

but I am getting unsupported encoding from the server response....

但我从服务器响应中得到不受支持的编码....

is there any other way to convert image to base64 string??

有没有其他方法将图像转换为base64字符串?

plz..help...

plz..help ...

my code is:

我的代码是:

        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),  R.drawable.image);

        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
        byte [] ba = bao.toByteArray();

         String ba1=Base64.encodeBytes(ba);

5 个解决方案

#1


15  

Please use this code..

请使用此代码..

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),  R.drawable.image);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

Please import

请导入

import android.util.Base64;

#2


3  

// convert from bitmap to byte array
public byte[] getBytesFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70, stream);
    return stream.toByteArray();
}

// get the base 64 string
String imgString = Base64.encodeToString(getBytesFromBitmap(someImg), 
                       Base64.NO_WRAP);

#3


2  

try below code

尝试以下代码

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte b [] = baos.toByteArray();
String base64String = Base64.encodeToString(b, Base64.DEFAULT);

Dont forget to import android.util.Base64;

别忘了导入android.util.Base64;

#4


1  

maybe you can try

也许你可以试试

String base64Result = Base64.encodeToString(yourByteArray[], Base64.DEFAULT);

String base64Result = Base64.encodeToString(yourByteArray [],Base64.DEFAULT);

#5


1  

Method:

方法:

private String bitmapToBase64(Bitmap bitmap) {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream .toByteArray();

    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

Imports:

进口:

import android.util.Base64;
import java.io.ByteArrayOutputStream;

#1


15  

Please use this code..

请使用此代码..

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),  R.drawable.image);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

Please import

请导入

import android.util.Base64;

#2


3  

// convert from bitmap to byte array
public byte[] getBytesFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70, stream);
    return stream.toByteArray();
}

// get the base 64 string
String imgString = Base64.encodeToString(getBytesFromBitmap(someImg), 
                       Base64.NO_WRAP);

#3


2  

try below code

尝试以下代码

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte b [] = baos.toByteArray();
String base64String = Base64.encodeToString(b, Base64.DEFAULT);

Dont forget to import android.util.Base64;

别忘了导入android.util.Base64;

#4


1  

maybe you can try

也许你可以试试

String base64Result = Base64.encodeToString(yourByteArray[], Base64.DEFAULT);

String base64Result = Base64.encodeToString(yourByteArray [],Base64.DEFAULT);

#5


1  

Method:

方法:

private String bitmapToBase64(Bitmap bitmap) {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream .toByteArray();

    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

Imports:

进口:

import android.util.Base64;
import java.io.ByteArrayOutputStream;