我正在尝试使用Base64格式的改造来上传图像。
将位图转换为Base64,
public static String convertImageToStringForServer(Bitmap imageBitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
if(imageBitmap != null) {
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 60, stream);
byte[] byteArray = stream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}else{
return null;
}
}
我不想使用Typedfile上传图片。
我的请求方法如下,
@Multipart
@POST("/pingpong")
void doPingpong(@Part ("access_token") TypedString accessToken,
@Part("image") TypedString profileImage,
@Part("pixels") TypedString doPingPong,Callback<pixelsPing> callback);
Base64转换是正确的,但我没有在服务器上获取图像。我在上面做错了什么?
答案 0 :(得分:4)
将您的Base64字符串发布为您的请求的Field参数。还可以使用FormUrlEncoded注释,它添加&#34; application / x-www-form-urlencoded&#34;内容类型。
@POST("/upload")
@FormUrlEncoded
void upload(@Field("image") String base64, Callback<ResponseObject> callback);
答案 1 :(得分:0)
模型类
public class MyModel {
@SerializedName("name")
String Name;
@SerializedName("image")
String Image;
@SerializedName("response")
String Response;
public MyModel(String name, String image, String response) {
Name = name;
Image = image;
Response = response;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getResponse() {
return Response;
}
public void setResponse(String response) {
Response = response;
}
}
界面
public interface MyInterFace {
@FormUrlEncoded
@POST("Filename.php")
Call<MyModel> imgUp(@Field("name") String name, @Field("image") String image);
}
主要活动
public class MainActivity extends AppCompatActivity {
EditText name;
ImageView imageView;
Button button, upload;
static final int IMG_REQ = 777;
Bitmap bitmap;``
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
imageView = findViewById(R.id.image);
button = findViewById(R.id.button);
upload = findViewById(R.id.button_upload);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectImage();
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
upMy();
}
});
}
public void upMy() {
String Img = imageToString();
String name3 = name.getText().toString();
MyInterFace interFace = ApiClient.getApi().create(MyInterFace.class);
Call<MyModel> call = interFace.imgUp(name3, Img);
call.enqueue(new Callback<MyModel>() {
@Override
public void onResponse(Call<MyModel> call, Response<MyModel> response) {
MyModel myModel = response.body();
imageView.setVisibility(View.GONE);
name.setVisibility(View.GONE);
button.setEnabled(true);
upload.setEnabled(false);
name.setText("");
Toast.makeText(MainActivity.this, myModel.getResponse(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<MyModel> call, Throwable t) {
}
});
}
private void selectImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, IMG_REQ);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMG_REQ && resultCode == RESULT_OK && data != null) {
Uri path = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), path);
imageView.setImageBitmap(bitmap);
imageView.setVisibility(View.VISIBLE);
name.setVisibility(View.VISIBLE);
button.setEnabled(false);
upload.setEnabled(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String imageToString() {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] imagByte = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imagByte, Base64.DEFAULT);
}
}
Apiclient
public class ApiClient {
public static final String BASE_URL = Your Base URL;
public static Retrofit retrofit;
public static Retrofit getApi() {
if (retrofit == null) {
retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
}
return retrofit;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp"
tools:context="shuan.sam.MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="5dp"
android:visibility="gone"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_below="@+id/image"
android:visibility="gone"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="Choose"
android:layout_below="@+id/name"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_upload"
android:text="Upload"
android:layout_below="@+id/button"
android:layout_marginTop="10dp"
android:enabled="false"
/>
</RelativeLayout>