如何使用Retrofit上传位图图像?

时间:2017-06-01 14:06:10

标签: android file-upload bitmap retrofit

在我的应用程序中,我需要让用户在将图像大小调整为Bitmaps后从库中上传6个图像,我已成功完成选择和调整大小部分,如下图所示,并将结果存储在位图数组中 imageslist 。 我的问题是如何使用Retrofit MultipartBody上传我的位图数组?关于通过文件路径上传文件上传的Retrofit的所有主题,就像这个问题的答案一样,我无法在调整大小之前直接上传文件

Retrofit Uploading multiple images to a single key

这是我的代码:

 private static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
        if (maxHeight > 0 && maxWidth > 0) {
            int width = image.getWidth();
            int height = image.getHeight();
            float ratioBitmap = (float) width / (float) height;
            float ratioMax = (float) maxWidth / (float) maxHeight;

            int finalWidth = maxWidth;
            int finalHeight = maxHeight;
            if (ratioMax > 1) {
                finalWidth = (int) ((float)maxHeight * ratioBitmap);
            } else {
                finalHeight = (int) ((float)maxWidth / ratioBitmap);
            }
            image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
            return image;
        } else {
            return image;
        }
    }
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ConstantsCustomGallery.REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
            //The array list has the image paths of the selected images
            ArrayList<Image> images = data.getParcelableArrayListExtra(ConstantsCustomGallery.INTENT_EXTRA_IMAGES);

            for (int i = 0; i < images.size(); i++) {

                Uri uri = Uri.fromFile(new File(images.get(i).path));
                Bitmap bm = BitmapFactory.decodeFile(images.get(i).path);
                Bitmap resized = resize(bm,512,512);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                resized.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                // ignore below line
                //String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
                imageslist.add(resized);
                // ignore below lines
                //TextSliderView textSliderView = new TextSliderView(uploadimages.this);
                //textSliderView.image(new File(images.get(i).path));
                //mslider.addSlider(textSliderView);


            }
}

非常感谢任何帮助

0 个答案:

没有答案