Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用改型将包含图像的类从Android发送到Java Restful Web服务器_Android_Image_Rest_Retrofit - Fatal编程技术网

如何使用改型将包含图像的类从Android发送到Java Restful Web服务器

如何使用改型将包含图像的类从Android发送到Java Restful Web服务器,android,image,rest,retrofit,Android,Image,Rest,Retrofit,我使用改型将数据(类)从android传输到服务器。通常我们在客户端和服务器端创建相同的类,其中包含int等数据类型,两边的字符串相同。现在我需要在类中将图像发送到服务器。我也不想只发送图像到服务器,但我想发送包含1个图像作为数据类型的类。那么我该怎么做呢?或者有什么建议,我该如何使用其他工具来执行此操作?假设您已经有一个指向图片: final File imageFile = ...; 注意:如何获取此文件取决于您是否只允许文件选择器中的本地文件(或例如,实际位于Google Drive中的

我使用改型将数据(类)从android传输到服务器。通常我们在客户端和服务器端创建相同的类,其中包含int等数据类型,两边的字符串相同。现在我需要在类中将图像发送到服务器。我也不想只发送图像到服务器,但我想发送包含1个图像作为数据类型的类。那么我该怎么做呢?或者有什么建议,我该如何使用其他工具来执行此操作?

假设您已经有一个指向图片:

final File imageFile = ...;
注意:如何获取此文件取决于您是否只允许文件选择器中的本地文件(或例如,实际位于Google Drive中的图像)以及您使用的Android版本等

要上传图像,您需要使用改装的
TypedFile
-以下是对我有效的方法:

@POST("/api/image/upload")
@Multipart
public void submitPictureToVoting(@Part("user_id") Integer userId,
                                  @Part("image_title") String imageTitle,
                                  @Part("file") TypedFile file,
                                  Callback<UploadImageResponse> callback);
@POST(“/api/image/upload”)
@多部分
public void submitpicturetoviting(@Part(“user_id”)整数userId,
@部分(“图像标题”)字符串图像标题,
@零件(“文件”)类型文件文件,
回调(回调);
然后:

final TypedFile typedImageFile = new TypedFile("application/octet-stream", imageFile);

mApiClient.submitPictureToVoting(
    1234567,
    "This is me!",
    typedImageFile,
    new Callback<UploadImageResponse> {
        // ...
    });
final TypedFile typedImageFile=新的TypedFile(“应用程序/八位字节流”,图像文件);
mApiClient.submitPictureToVoting(
1234567,
“这就是我!”,
typedImageFile,
新回调{
// ...
});

我认为这不是答案,但通过这样做,我们可以轻松解决改装问题

您好,我尝试通过将图像编码为字符串,这样我们就可以像其他字符串一样轻松地发送此“字符串”数据类型&当我们想要返回图像时,我们可以对其进行解码

1.编码代码

这里是从ImageView拍摄的图像

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
2.解码代码

 byte[] decodedByte = Base64.decode(encodedImage, 0);
 Bitmap decodedImg= BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
 imageView.setImageBitmap(decodedImg);
  
MiHOLA考虑“任意格式”的图像,因为在我的场景中,我将手机上的图像附加到服务器上。