android 如何在Retrofit 2中上传图像文件

nzk0hqpo  于 2022-12-28  发布在  Android
关注(0)|答案(9)|浏览(238)

我有一个像下面的 Postman 的图像。我怎么能做同样的事情在改造2?

我已经声明了如下的接口:

@Multipart
@POST("/api/Pharmarcy/UploadImage")
Call<ResponseBody> uploadPrescriptionImage(
        @Query("accessToken") String token,
        @Query("pharmarcyRequestId") int pharmacyRequestedId,
        @Part MultipartBody.Part image);
zte4gxcn

zte4gxcn1#

@Multipart
@POST("user/updateprofile")
Observable<ResponseBody> updateProfile(@Part("user_id") RequestBody id,
                                       @Part("full_name") RequestBody fullName,
                                       @Part MultipartBody.Part image,
                                       @Part("other") RequestBody other);

//pass it like this
File file = new File("/storage/emulated/0/Download/Corrections 6.jpg");
RequestBody requestFile =
        RequestBody.create(MediaType.parse("multipart/form-data"), file);

// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
        MultipartBody.Part.createFormData("image", file.getName(), requestFile);

// add another part within the multipart request
RequestBody fullName = 
        RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name");

service.updateProfile(id, fullName, body, other);

看看我传递多部分和字符串参数的方式。希望这对你有帮助!

jslywgbw

jslywgbw2#

对于那些拥有inputStream的用户,可以使用Multipart上传inputStream。

@Multipart
@POST("pictures")
suspend fun uploadPicture(
        @Part part: MultipartBody.Part
): NetworkPicture

然后在您的存储库类中:

suspend fun upload(inputStream: InputStream) {
   val part = MultipartBody.Part.createFormData(
         "pic", "myPic", RequestBody.create(
              MediaType.parse("image/*"),
              inputStream.readBytes()
          )
   )
   uploadPicture(part)
}

如果后端不允许多部分,可以将输入流转换为字节,并将字节数组作为请求主体发送,如下所示。

// In your service
 @PUT
 suspend fun s3Upload(
     @Header("Content-Type") mime: String,
     @Url uploadUrl: String, 
     @Body body: RequestBody 
 )
// In your repository
val body = RequestBody.create(MediaType.parse("application/octet"), inputStream.readBytes())
networkService.s3Upload(mime, url, body)

要获得一个输入流,您可以这样做。
在你的fragment或activity中,你需要创建一个返回InputStream的图像拾取器,InputStream的优点是它可以用于google drive和dropbox等云端文件。
View.OnClickListeneronOptionsItemSelected调用pickImagesLauncher.launch("image/*")。(请参见Activity Result APIs)。

private val pickImagesLauncher =
           registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
                uri?.let {
                    val stream = contentResolver.openInputStream(it)
                    itemViewModel.uploadPicture(stream)
                }
            }

override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)

      btn.setOnClickListener {
         pickImagesLauncher.launch("image/*")
     }
 }
vyswwuz2

vyswwuz23#

**上传图像请参阅此处单击此链接

**

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

class AppConfig {

    private static String BASE_URL = "http://mushtaq.16mb.com/";
    
    static Retrofit getRetrofit() {

        return new Retrofit.Builder()
                .baseUrl(AppConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
}

========================================================
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;

interface ApiConfig {
    @Multipart
    @POST("retrofit_example/upload_image.php")
    Call<ServerResponse> uploadFile(@Part MultipartBody.Part file,
                                    @Part("file") RequestBody name);

    /*@Multipart
    @POST("ImageUpload")
    Call<ServerResponseKeshav> uploadFile(@Part MultipartBody.Part file,
                                    @Part("file") RequestBody name);*/

    @Multipart
    @POST("retrofit_example/upload_multiple_files.php")
    Call<ServerResponse> uploadMulFile(@Part MultipartBody.Part file1,
                                       @Part MultipartBody.Part file2);
}
hrysbysz

hrysbysz4#

我完全同意@tir38和@android_griezmann的观点,这是Kotlin的版本:

interface servicesEndPoint {
@Multipart
@POST("user/updateprofile")
fun updateProfile(@Part("user_id") id:RequestBody, @Part("full_name") fullName:RequestBody, @Part image: MultipartBody.Part, @Part("other") other:RequestBody): Single<UploadPhotoResponse>

companion object {
        val API_BASE_URL = "YOUR_URL"

        fun create(): servicesPhotoEndPoint {
            val retrofit = Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(API_BASE_URL)
                .build()
            return retrofit.create(servicesPhotoEndPoint::class.java)
        }
    }
}

// Pass it like this
val file = File(RealPathUtils.getRealPathFromURI_API19(context, uri))
val requestFile: RequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)

// MultipartBody.Part is used to send also the actual file name
val body: MultipartBody.Part = MultipartBody.Part.createFormData("image", file.name, requestFile)

// Add another part within the multipart request
val fullName: RequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name")

servicesEndPoint.create().updateProfile(id, fullName, body, fullName)

要获取真实的路径,请使用RealPathUtils。在@Harsh Bhavsar的答案中检查此类:***
要 * 从URI_API19获取真实路径 *,您需要 * 读取外部存储 * 的权限。

xmjla07d

xmjla07d5#

使用Retrofit 2.0,您可以使用以下命令:

@Multipart
    @POST("uploadImage")
    Call<ResponseBody> uploadImage(@Part("file\"; fileName=\"myFile.png\" ")RequestBody requestBodyFile, @Part("image") RequestBody requestBodyJson);

提出请求:

File imgFile = new File("YOUR IMAGE FILE PATH");
RequestBody requestBodyFile = RequestBody.create(MediaType.parse("image/*"), imgFile);
RequestBody requestBodyJson = RequestBody.create(MediaType.parse("text/plain"),
                    retrofitClient.getJsonObject(uploadRequest));


//make sync call
Call<ResponseBody> uploadBundle = uploadImpl.uploadImage(requestBodyFile, requestBodyJson);
Response<BaseResponse> response = uploadBundle.execute();

请参考https://square.github.io/retrofit/

x7yiwoj4

x7yiwoj46#

@Multipart
@POST(Config.UPLOAD_IMAGE)
Observable<Response<String>> uploadPhoto(@Header("Access-Token") String header, @Part MultipartBody.Part imageFile);

你可以这样调用这个API:

public void uploadImage(File file) {
     // create multipart
     RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);

    // upload
    getViewInteractor().showProfileUploadingProgress();

    Observable<Response<String>> observable = api.uploadPhoto("",body);

    // on Response
    subscribeForNetwork(observable, new ApiObserver<Response<String>>() {
        @Override
        public void onError(Throwable e) {
            getViewInteractor().hideProfileUploadingProgress();
        }

        @Override
        public void onResponse(Response<String> response) {

            if (response.code() != 200) {
                Timber.d("error " + response.code());
                return;
            }
            getViewInteractor().hideProfileUploadingProgress();
            getViewInteractor().onProfileImageUploadSuccess(response.body());

        }
    });

}
2sbarzqh

2sbarzqh7#

改造2.0解决方案

@Multipart
@POST(APIUtils.UPDATE_PROFILE_IMAGE_URL)
public Call<CommonResponse> requestUpdateImage(@PartMap Map<String, RequestBody> map);

以及

Map<String, RequestBody> params = new HashMap<>();

    params.put("newProfilePicture" + "\"; filename=\"" + FilenameUtils.getName(file.getAbsolutePath()), RequestBody.create(MediaType.parse("image/jpg"), file));


 Call<CommonResponse> call = request.requestUpdateImage(params);

您可以使用
图片/jpg图片/png图片/gif

ifmq2ha2

ifmq2ha28#

这很简单。下面是API接口

public interface Api {

    @Multipart
    @POST("upload")
    Call<MyResponse> uploadImage(@Part("image\"; filename=\"myfile.jpg\" ") RequestBody file, @Part("desc") RequestBody desc);

}

并且可以使用下面的代码进行调用。

private void uploadFile(File file, String desc) {

        //creating request body for file
        RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)), file);
        RequestBody descBody = RequestBody.create(MediaType.parse("text/plain"), desc);

        //The gson builder
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        //creating retrofit object
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        //creating our api 
        Api api = retrofit.create(Api.class);

        //creating a call and calling the upload image method 
        Call<MyResponse> call = api.uploadImage(requestFile, descBody);

        //finally performing the call 
        call.enqueue(new Callback<MyResponse>() {
            @Override
            public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
                if (!response.body().error) {
                    Toast.makeText(getApplicationContext(), "File Uploaded Successfully...", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Some error occurred...", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<MyResponse> call, Throwable t) {
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }

资料来源:Retrofit Upload File Tutorial

lf5gs5x2

lf5gs5x29#

如果您想在Multipart/formData中发送具有更多不同参数的图像,请使用下面的代码。它将解决您的问题。

` **1) Put this line above onCreate() method.**

    File selectedFile = null;
    
    **2) In onActivityResult() method of Camera Intent put this below code -**

    final Bitmap photo = (Bitmap) data.getExtras().get("data");
                imageViewPhoto.setImageBitmap(photo);
                selectedFile = new File(this.getFilesDir(), "image" + ".jpg");
                FileOutputStream outputStream = null;
                try {
                    outputStream = new FileOutputStream(selectedFile);
                    photo.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                    outputStream.flush();
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

**3) In API Interface Class -**

 @Multipart
    @POST("photouploadWithdata.php")
    Call<YourResponseModal> startDuty(@PartMap HashMap<String, RequestBody> map, @Part MultipartBody.Part image);

**4) In Activity when you call API-** 

 RequestBody tokens = RequestBody.create(MediaType.parse("multipart/form-data"), token);
        RequestBody dates = RequestBody.create(MediaType.parse("multipart/form-data"), date);
        RequestBody times = RequestBody.create(MediaType.parse("multipart/form-data"), time);
        RequestBody latitudes = RequestBody.create(MediaType.parse("multipart/form-data"), latitude);
        RequestBody longitudes = RequestBody.create(MediaType.parse("multipart/form-data"), longitude);

        MultipartBody.Part image = MultipartBody.Part.createFormData("avatar", selectedFile.getName(),
                RequestBody.create(MediaType.parse(URLConnection.guessContentTypeFromName(selectedFile.getName())), selectedFile));

        HashMap<String, RequestBody> map = new HashMap<>();
        map.put("token", tokens);
        map.put("date", dates);
        map.put("time_in", times);
        map.put("latitude", latitudes);
        map.put("longitude", longitudes);

        RetrofitAPI retrofitAPI = APIClient.getRetrofitInstance().create(RetrofitAPI.class);
        Call<StartDutyResponseModal> call = retrofitAPI.startDuty(map, image);
        call.enqueue(new Callback<StartDutyResponseModal>() {
            @Override
            public void onResponse(Call<StartDutyResponseModal> call, Response<StartDutyResponseModal> response) {
                if (response.body().getStatus() == true){
                    progressBar.setVisibility(View.GONE);
                    Intent intent = new Intent(StartDutyActivity.this, StoreListActivity.class);
                    startActivity(intent);
                    finish();
                }

                else {
                    progressBar.setVisibility(View.GONE);
                    AppUtils.showToast(response.body().getMessage(), StartDutyActivity.this);
                }
            }

            @Override
            public void onFailure(Call<StartDutyResponseModal> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
                AppUtils.showToast(t.getMessage(),StartDutyActivity.this);
            }
        });
    }

`

相关问题