如何通过改造上传位图图像?

时间:2018-03-31 19:44:25

标签: android retrofit2 image-uploading capture

捕获的图像采用BITMAP格式。通常我们会在将图像上传到服务器之前压缩图像。我需要上传图像而不压缩。请找到以下代码:

if (CAMERA_PIC_REQUEST == 11) 
{
    switch (resultCode)
    {
        case CertificationAddOwnerImage.RESULT_OK:
            if(CAMERA_PIC_REQUEST==11)
            {
                Bitmap myBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
                click_img.setImageBitmap(myBitmap);
                //onCaptureImageResult(data);
            }
            else
            {
                Toast.makeText(this, "The file was saved at"+imageFile.getAbsolutePath(), 
                Toast.LENGTH_SHORT).show();
            }
            break;

        case Activity.RESULT_CANCELED:
            break;
    }
}

1 个答案:

答案 0 :(得分:0)

你可以尝试获取位图的字节数组

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

然后通过请求正文

上传字节数组
RequestBody requestBody = RequestBody
        .create(MediaType.parse("application/octet-stream"), byteArray);
相关问题