android - 如何在 Android 中将图像转换为字符串?

标签 android android-image

It's my MySql DB its my php page that is read streareader 它的编码:-

public String convertBitmapToString(Bitmap bmp) {
   ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); //compress to which format you want.
        byte[] byte_arr = stream.toByteArray();
        String imageStr = Base64.encodeToString(byte_arr, 1);
        return imageStr;
}

这是解码:-

String img=o.toString();
   byte[] imageAsBytes = Base64.decode(img.getBytes(), Base64.DEFAULT);
 imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));

谢谢。

最佳答案

这可以通过

private String getBase64String() {

    // give your image file url in mCurrentPhotoPath
    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    // In case you want to compress your image, here it's at 40%
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();

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

解码

private void decodeBase64AndSetImage(String completeImageData, ImageView imageView) {

    // Incase you're storing into aws or other places where we have extension stored in the starting.
    String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1);

    InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));

    Bitmap bitmap = BitmapFactory.decodeStream(stream);

    imageView.setImageBitmap(bitmap);
}

关于android - 如何在 Android 中将图像转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41396194/

相关文章:

java - SMS_RECEIVED onReceive android 2.3.5 不触发

java - 将图像序列化为字符串

android - Eclipse能否为Android项目自动生成不同的DPI图片?

android - horizo​​ntalscrollview - onScroll?

android - 在Android上使用Youtube Api播放新视频

android - 小写初始键盘

android - Android 中相机图像返回缩略图

android - 不能对图像使用 parseInt - Android

android - 更改笔划颜色会更改以前的笔划颜色

java - 进度条在图像出现之前消失