【问题标题】:Base64 encode is becoming a piece of the imageBase64 编码正在成为图像的一部分
【发布时间】:2016-03-08 22:13:15
【问题描述】:

我正在尝试从图库中获取图像并将其发送到服务器。
我确实得到了一个 base64 编码的字符串,但它是一条细线,而不是整个图像。

比如我用Motobit解码this random image.得到的base64字符串 工作正常。但是我从我的应用程序中获得的相同图像的编码字符串确实更小,当您转换为图像时,它变为this.

这是我的代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_profile);


    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch(requestCode) {
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){
                try {
                    Uri selectedImage = imageReturnedIntent.getData();

                    String imageStream = getRealPathFromURI(context, selectedImage);
                    Bitmap bitmap = BitmapFactory.decodeFile(imageStream);

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

                    Log.e(LOG_TAG, encodedString);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
    }
}

public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}



是什么让我的encodedString 成为图像的一部分?谢谢!

【问题讨论】:

  • 您没有展示如何解码您的编码字符串。那么我们如何评论哪里出了问题呢?这哪里出错了?你没说。你应该告诉字节数组的大小和字符串的长度。更小?准确无误。
  • @greednapps 我正在这个网站上解码:codebeautify.org/base64-to-image-converter
  • @greenapps 因为当我以这种方式发送到服务器时,它会作为损坏的图像。

标签: android image bitmap base64


【解决方案1】:

也许字符串是正确的,但 Log 方法有字符限制。

【讨论】:

    猜你喜欢
    • 2016-05-22
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 2017-01-09
    • 2022-11-30
    相关资源
    最近更新 更多