图像必须是有效的base64格式,文件类型必须是.jpg格式错误

zzoitvuj  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(257)

好的,这是我第一次将图像从gallery发送到服务器。我使用SharedPreferences将图像从fragment发送到viewmodel。我的服务器希望它是字符串。我将它作为正文发送,其中包含我的服务器例外的不同数据。但是当我尝试发送图像时,我遇到了此错误。
我的碎片`

@Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && data != null) {

            Uri selectedImageUri = data.getData();
            imgUriString = selectedImageUri.toString();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), selectedImageUri);
                binding.addReceiptIB.setImageBitmap(bitmap);

            } catch (IOException e) {
                e.printStackTrace();
            }
            SharedPref.setImagePath(bitMapToString(bitmap));

//            //send this encoded string to server
        //    imgUriString = selectedImageUri.toString();
//                binding.addReceiptIB.setImageURI(selectedImageUri);

        }
    }

public String bitMapToString(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

`在我的视图模型中,我发送的图像如下:

private Map<String, Object> body() {
    model.setMonth(month);
    model.setYear(year);

    HashMap<String, Object> map = new HashMap<>();
    map.put("year", model.getYear());
    map.put("month", model.getMonth());
    map.put("memberId", model.getMemberId());
    map.put("expenseCategoryId", model.getExpenseCategoryId());
    map.put("expensePlace", model.getExpensePlace());
    map.put("date", model.getDate());
    map.put("amount", model.getAmount());
    map.put("note", model.getNote());
    map.put("image", model.getImage());
    Log.i("Sadrzaj mape", map.toString());
    return map;
}

这也是我在viewmodel中接收图像的方式:

public void onCickAddReceipt(View view) {
        model.setImage(SharedPref.getImagePath());
        callBack.onCickAddReceipt();
    }

奇怪的是我有一些基本的64

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA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