如果用户不更新 Android Studio 中的图像,则在更新详细信息时出错

Getting error on updating details if the user won't update the image in Android Studio

我有这个片段 UserProfileFragment.java,这里我有一个表单,其中包含一些字段,还有一个用户配置文件图像视图,用户可以在其中上传其图像,最初一切正常,但是当用户想要更新从服务器获取数据后的详细信息还在图像视图上获取和设置图像的缩略图,这里当用户更新详细信息而不更改图像视图的当前个人资料图像时,应用程序崩溃并给出如下错误:

FATAL EXCEPTION: main
    Process: com.***.******, PID: 16313
    java.lang.NullPointerException
        at java.io.File.<init>(File.java:283)
        at com.***.*****.fragment.UserProfileFragment.Updatemyprofile(UserProfileFragment.java:576)
        at com.***.*****.fragment.UserProfileFragment.onClick(UserProfileFragment.java:343)

当用户更改个人资料图片时,应用不会在更新时崩溃。

我的更新用户方法如下:

private void Updatemyprofile() {
//pass it like this
        File file = new File(picturePath);
        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 userid = RequestBody.create(MediaType.parse("multipart/form-data"), Constant.Userid);
        RequestBody role = RequestBody.create(MediaType.parse("multipart/form-data"), Constant.Role);
        RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), edtfullName.getText().toString().trim());
        RequestBody pincode = RequestBody.create(MediaType.parse("multipart/form-data"), edtpincode.getText().toString().trim());
        RequestBody address = RequestBody.create(MediaType.parse("multipart/form-data"), edtaddress.getText().toString().trim());
        RequestBody gender = RequestBody.create(MediaType.parse("multipart/form-data"), Gender);
        RequestBody dob = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(epoch));

        apiInterface = RetrofitApiUtils.getAPIService();
        apiInterface.Updatemyprofile(Constant.AccessToken, userid, role, name, pincode, address, gender, dob, body ).enqueue(new Callback<UpdateUserProf>() {
            @Override
            public void onResponse(@NonNull Call<UpdateUserProf> call, @NonNull Response<UpdateUserProf> response) {
                try {
                    if (response.isSuccessful()) {
                        if (response.body() != null && response.body().getStatus().equals(true)) {
                            Toast.makeText(getActivity(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
                            UpdateUserProfData updateUserProfData = response.body().getData();
                            Log.d("Results ==>>...1", new GsonBuilder().setPrettyPrinting().create().toJson(updateUserProfData));
                            myprofile(Constant.Userid, Constant.Role);
                        } else {
                            Toast.makeText(getActivity(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        CallingToastMethod.showToast(getActivity());
                    }
                } catch (Exception e) {
                    try {
                        throw new IOException(e.toString());
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<UpdateUserProf> call, Throwable t) {
                Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

我也尝试过这种图片验证方法:

  if (imgprofile.getDrawable() == null){
                        Toast.makeText(getActivity(), "Please select Image", Toast.LENGTH_SHORT).show();
                    }

当用户第一次输入详细信息但不更新它时它起作用。

我的界面class方法如下:

// Update My Profile
    @Multipart
    @POST("updatemyprofile.php")
        Call<UpdateUserProf> Updatemyprofile(@Header("Authorization") String authkey,
                                             @Part("id") RequestBody id,
                                             @Part("role") RequestBody role,
                                             @Part("name") RequestBody name,
                                             @Part("pincode") RequestBody pincode,
                                             @Part("address") RequestBody address,
                                             @Part("gender") RequestBody gender,
                                             @Part("dob") RequestBody dob,
                                             @Part MultipartBody.Part image);

请告诉我该怎么做我已经坚持了好几天不知道该怎么做... 谢谢!注意安全..

可能是你的图片路径没有找到

if条件内尝试:

if(picturePath != null){
File file = new File(picturePath);
        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);
}```