Android将Bitmap转换为Base64字符串问题

时间:2017-05-07 09:03:05

标签: android image bitmap base64

我将Bitmap转换为Base64时遇到问题。我想从相机或画廊发布一些图像。当我将它们作为图像并且我必须转换为base64但是当我获得字符串并使用http://codebeautify.org/base64-to-image-converter进行测试以查看我的图像时。结果看起来质量很差。我该如何解决这个问题对我有帮助吗? 这是我的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch(requestCode)
    {
        case 0:
            if(resultCode == RESULT_OK)
            {
                Bundle extras = imageReturnedIntent.getExtras();
                Bitmap selectedImage = (Bitmap)extras.get("data");
                secilenImage.setImageBitmap(selectedImage);
                String encodedImage;
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                try
                {
                    selectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] b = stream.toByteArray();
                    encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
                    images.add(i, encodedImage);
                    Log.d("StringForCamera: ", encodedImage);
                    stream.close();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
                secilenImage.setImageBitmap(selectedImage);
                secilenImage.setEnabled(false);
            }
            break;
        case 1:
            if(resultCode == RESULT_OK)
            {
                Uri imageUri = imageReturnedIntent.getData();
                Bitmap selectedImage = null;
                String encodedImage;
                try
                {
                    selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
                    ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
                    selectedImage.compress(Bitmap.CompressFormat.PNG, 100, byteArrayBitmapStream);
                    byte[] b = byteArrayBitmapStream.toByteArray();
                    encodedImage = Base64.encodeToString(b, Base64.NO_WRAP);
                    images.add(i, encodedImage);
                    Log.d("StringForGallery: ",encodedImage);
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
                secilenImage.setImageBitmap(selectedImage);
                secilenImage.setEnabled(false);
            }
            break;
    }
    ++i;
}

1 个答案:

答案 0 :(得分:0)

 (Bitmap)extras.get("data");

这只是图像的缩略图。因此质量低。