在Android中,将图像转换为Base64最有效的方法是什么?

在Android中,将图像转换为Base64最有效的方法是什么?,android,base64,type-conversion,android-image,Android,Base64,Type Conversion,Android Image,我正在寻找在Android中将图像文件转换为Base64字符串的最有效方法 映像必须以单个Base64字符串一次发送到后端 首先我使用imageToByteArray,然后使用imageToBase64来获取字符串 public static byte[] imageToByteArray(String ImageName) throws IOException { File file = new File(sdcard, ImageName); InputStream

我正在寻找在Android中将图像文件转换为Base64字符串的最有效方法

映像必须以单个Base64字符串一次发送到后端

首先我使用imageToByteArray,然后使用imageToBase64来获取字符串

    public static byte[] imageToByteArray(String ImageName) throws IOException {
    File file = new File(sdcard, ImageName);
    InputStream is = new FileInputStream(file);

    // Get the size of the file
    long length = file.length();

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }

    //Close input stream
    is.close();
    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        throw new IOException("Could not completely read file "+file.getName());
    }
    return bytes;
}

    public String imageToBase64(String ImageName){      
    String encodedImage = null;
    try {
        encodedImage = Base64.encodeToString(imageToByteArray(ImageName), Base64.DEFAULT);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return encodedImage;
}
公共静态字节[]imageToByteArray(字符串ImageName)引发IOException{
文件文件=新文件(SD卡,图像名称);
InputStream is=新文件InputStream(文件);
//获取文件的大小
long length=file.length();
//创建字节数组以保存数据
字节[]字节=新字节[(int)长度];
//读入字节
整数偏移=0;
int numRead=0;
while(偏移量=0){
偏移量+=numRead;
}
//关闭输入流
is.close();
//确保已读入所有字节
if(偏移量<字节长度){
抛出新IOException(“无法完全读取文件”+file.getName());
}
返回字节;
}
公共字符串imageToBase64(字符串ImageName){
字符串encodedImage=null;
试一试{
encodedImage=Base64.encodeToString(imageToByteArray(ImageName),Base64.DEFAULT);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回图像;
}

下面是我处理它的主要方式,这是在调用图像选取器活动后的gotActivityResults回调中。它与您的类似,但我认为它会更有效,因为流中的toByteArray是它背后的本机c代码,而不是您的java循环

                       Uri selectedImage = imageReturnedIntent.getData();
                       InputStream imageStream = getContentResolver().openInputStream(selectedImage);
                       Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                       ByteArrayOutputStream bao = new ByteArrayOutputStream();

                       yourSelectedImage.compress(Bitmap.CompressFormat.JPEG, 90, bao);

                       byte [] ba = bao.toByteArray();

                       String ba1= Base64.encodeToString(ba, 0);

                       HashMap<String, String > params = new HashMap<String, String>();
                       params.put("avatar", ba1);
                       params.put("id", String.valueOf(uc.user_id));
                       params.put("user_id", String .valueOf(uc.user_id));
                       params.put("login_token", uc.auth_token);
                       uc.setAvatar(params);
uriselectedimage=imageReturnedIntent.getData();
InputStream imageStream=getContentResolver().openInputStream(SelecteImage);
位图您选择的edimage=BitmapFactory.decodeStream(图像流);
ByteArrayOutputStream bao=新建ByteArrayOutputStream();
选择edimage.compress(Bitmap.CompressFormat.JPEG,90,bao);
字节[]ba=bao.toByteArray();
字符串ba1=Base64.encodeToString(ba,0);
HashMap params=新的HashMap();
参数put(“化身”,ba1);
参数put(“id”,String.valueOf(uc.user_id));
参数put(“user_id”,String.valueOf(uc.user_id));
参数put(“登录令牌”,uc.auth令牌);
uc.setAvatar(参数);

您可以获取字节数组并将其拆分为几个子数组,然后在单独的线程中对它们进行编码,然后再将结果合并到一起以获得最终字符串。您正在将位图加载到内存中,这会消耗大量内存。我以前有一个和你差不多的代码,现在我把它改成了OP上的代码。