【问题标题】:compress bitmap to string android将位图压缩为字符串android
【发布时间】:2013-06-09 09:36:00
【问题描述】:

大家好,当我将图像编码为位图然后通过 Internet 使用工具与解码编码图像进行比较时,我遇到了一个问题,但没有给我相同的解码,并且无法弄清楚原因。

String path = "/sdcard/bluetooth/bluetooth.png"; 
Bitmap bitmap = BitmapFactory.decodeFile(path);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

【问题讨论】:

    标签: android bitmap base64 storage encode


    【解决方案1】:

    我使用下面的编码和解码,它对我来说很好

    编码

    public static String encodeTobase64(Bitmap image)
    {
      Bitmap immagex=image;
      ByteArrayOutputStream baos = new ByteArrayOutputStream();  
      immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
      byte[] b = baos.toByteArray();
      String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);  
      Log.e("LOOK", imageEncoded);
      return imageEncoded;
    }
    

    尝试下面的解码并将生成的位图设置为 imageview 并检查原始位图。

    解码。

    public static Bitmap decodeBase64(String input) 
    {
        byte[] decodedByte = Base64.decode(input, 0);
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
    }
    

    【讨论】:

    • 我有图像所在的文件路径,我必须像我一样转换位图......从外部存储器中获取它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多