无法通过改造将图像上传到服务器

Not able to upload image to server with retrofit

我目前正在做一个个人项目,我想在其中将个人资料照片上传到我的服务器。我可以很容易地通过邮递员将图像以表单数据形式发送到服务器,但是当我对改造做同样的事情时它不起作用,有时它会创建一个 1kb 的文件,打开时显示错误。

我目前正在做的是,

 Uri uri = data.getData();
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                profileImage.setImageBitmap(bitmap);


        
                File file = new File(uri.getPath());
                RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"),file);
                MultipartBody.Part body = MultipartBody.Part.createFormData("Profile", "bcs.jpeg", reqFile);
                authViewModel.setNewProfilePic(layout,email,body);

setNewProfilePic 的代码是,

public void setNewProfilePic(RelativeLayout relativeLayout, String email,MultipartBody.Part part) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        api = new Retrofit.Builder().baseUrl(Constants.URL_API).client(client).build().create(SpendistryAPI.class);
        retrofit2.Call<okhttp3.ResponseBody> req = api.setNewProfilePic(email,part);
        req.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                // Do Something
                if (!response.isSuccessful()) {
                    Toast.makeText(application, "notWorking: " + response.code(), Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
                    Toast.makeText(application, response.body().string(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(application, "catch", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
            }
        });

如果我将文件提供给 RequestBody.create 喜欢,

File file = new File(uri.getPath());
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"),file);

设置新个人资料图片的代码不会被执行,如果我在 .create() 方法中仅传递 URI.getPath() id,则会在服务器上创建无法打开的 1kb 图片文件。

请帮助我解决这种情况,在此先感谢!!

我认为你可能没有给读外部存储权限,uri.getPath()不会工作,你必须从 uri 找到一个真正的路径,你可以使用这个代码

private String getRealPathFromURI(Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String result = cursor.getString(column_index);
        cursor.close();
        return result;
    }

现在,将这个字符串传递给文件,否则代码就完美了!

FILE file = new file(getRealPathFromURI(uri));