【问题标题】:How to upload photo via retrofit from android device?如何通过改造从安卓设备上传照片?
【发布时间】:2023-03-15 07:50:01
【问题描述】:

我有这样的端点:

图片上传:
https://api.imgbb.com/1/upload

参数:
密钥(必需):API 密钥。
图像(必需):二进制文件、base64 数据或图像的 URL。 (最大 16MB)
name (optional) : 文件的名称,如果使用 POST 上传文件,则会自动检测到该名称 和多部分/表单数据

我想通过改造从安卓设备上传照片,所以我尝试了这个:

public class PhotoNetworkClient {

    public static final String KEY_API="my_key";
    private static final String BASE_URL = "https://api.imgbb.com/1/";
    private static Retrofit retrofit;

    public static Retrofit getRetrofitClient(Context context) {
        if (retrofit == null) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .build();
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}
public interface PhotoService {
    @Multipart()
    @POST("/upload")
    Call<ResponseBody> uploadImage(@Query("key") String key, @Part() MultipartBody.Part file );
}

我正在使用这个:

public static void testExecute(Bitmap bitmap, Context context) throws IOException {
    //Convert bitmap to byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapData = bos.toByteArray();
    //create file
    String currentDate = new SimpleDateFormat("ddMMyyyy_HHmmss", Locale.getDefault()).format(new Date());
    File f = new File(context.getCacheDir(), "temp_"+currentDate );
    f.createNewFile();
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(bitmapData);
    fos.flush();
    fos.close();

    Retrofit retrofit = PhotoNetworkClient.getRetrofitClient(context);
    PhotoService uploadAPIs = retrofit.create(PhotoService.class);

    RequestBody requestFile =  RequestBody.create(MediaType.parse("multipart/form-data"), f);
    MultipartBody.Part body = MultipartBody.Part.createFormData("image", f.getName(), requestFile);
    Call call = uploadAPIs.uploadImage(PhotoNetworkClient.KEY_API,body);
    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {
            if (response.isSuccessful())
                Toast.makeText(context, "I send photo", Toast.LENGTH_SHORT);
            else
                Toast.makeText(context, "response isn't successful", Toast.LENGTH_SHORT);
        }
        @Override
        public void onFailure(Call call, Throwable t) {
            Toast.makeText(context, "on failure", Toast.LENGTH_SHORT);
        }
    });
}

});

我收到代码为 200 的响应,但图片没有上传到服务器

【问题讨论】:

    标签: java android retrofit image-uploading


    【解决方案1】:

    这很晚,但我实际上也在寻找一些使用 ImgBB API 的示例。

    我最终设法上传了图片 - 我使用的代码与您的代码非常相似,但只是稍作改动:

    不要在基本 URL 中包含“1”参数,将其保留在 POST 中。这为我解决了问题。

    BASE_URL = "https://api.imgbb.com"
    
    @POST("/1/upload")
    

    【讨论】:

      猜你喜欢
      • 2016-04-23
      • 2010-12-10
      • 2011-08-15
      • 2014-06-23
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多