Android 如何通过改装上传位图图像?

Android 如何通过改装上传位图图像?,android,file-upload,bitmap,retrofit,Android,File Upload,Bitmap,Retrofit,在我的应用程序中,我需要允许用户从gallery最多上传6幅图像。在将图像调整为位图后,我已成功完成选择和调整大小部分,如下代码所示,并将结果存储在位图数组imageslist。 我的问题是如何使用改装的MultipartBody上传我的位图阵列??所有关于改型的话题都是关于通过文件路径上传文件,就像这个问题的答案一样,我不能在调整大小之前直接上传文件 这是我的代码: private static Bitmap resize(Bitmap image, int maxWidth, int m

在我的应用程序中,我需要允许用户从gallery最多上传6幅图像。在将图像调整为位图后,我已成功完成选择和调整大小部分,如下代码所示,并将结果存储在位图数组imageslist。 我的问题是如何使用改装的MultipartBody上传我的位图阵列??所有关于改型的话题都是关于通过文件路径上传文件,就像这个问题的答案一样,我不能在调整大小之前直接上传文件

这是我的代码:

 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);


            }
}
private静态位图调整大小(位图图像、int-maxWidth、int-maxHeight){
如果(maxHeight>0&&maxWidth>0){
int width=image.getWidth();
int height=image.getHeight();
浮动比率OBITMAP=(浮动)宽度/(浮动)高度;
浮动比率最大=(浮动)最大宽度/(浮动)最大高度;
int finalWidth=最大宽度;
int finalHeight=最大高度;
如果(比率最大值>1){
finalWidth=(int)((float)maxHeight*ratioBitmap);
}否则{
最终高度=(int)((浮点)最大宽度/比率映射);
}
image=Bitmap.createScaledBitmap(图像,最终宽度,最终高度,真);
返回图像;
}否则{
返回图像;
}
}
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==ConstantsCustomGallery.REQUEST\u CODE&&resultCode==Activity.RESULT\u OK&&data!=null){
//阵列列表包含选定图像的图像路径
ArrayList images=data.getParcelableArrayListXTRA(ConstantCustomGallery.INTENT\u EXTRA\u images);
对于(int i=0;i

任何帮助都将不胜感激

因此您只需将所有图像作为多部分上传。对吗?是的,但我必须在上传图像之前调整图像大小任何帮助都可以。您只需将所有图像作为多部分上传。对吗?是的,但我必须在上传图像之前调整图像大小任何帮助都可以