gpt4 book ai didi

android - MultipartBody.Part上传文件

转载 作者:行者123 更新时间:2023-11-29 23:41:16 28 4
gpt4 key购买 nike

我正在使用改造 2 上传我的文件,我遇到的一个问题是当我尝试从 URI 获取文件时出现错误:

W/System.err: stat failed: ENOENT (No such file or directory) : /external/images/media/1838
stat failed: ENOENT (No such file or directory) : /external/images/media/1838
stat failed: ENOENT (No such file or directory) : /external/images/media/1838

我在日志中的无限循环中遇到了这个错误。我为从画廊获取图像而编写的代码:

        imageViewUpdateProfileActivity.setOnClickListener(view -> {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
});

并在 onActivityResult 中获取它:

    @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
File file = new File(Objects.requireNonNull(Objects.requireNonNull(data).getData()).getPath());
MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
RequestBody fullname = RequestBody.create(MediaType.parse("text/plain"), editTextFullNameUpdateProfileActivity.getText().toString());
RequestBody Gender = RequestBody.create(MediaType.parse("text/plain"), gender);
RequestBody aboutMe = RequestBody.create(MediaType.parse("text/plain"), editTextAboutMeUpdateProfileActivity.getText().toString());

ApiClient.getClient().create(ApiService.class).updateProfilePhoto(App.userProfile.getUsername(), filePart, fullname, Gender, aboutMe).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}

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

最佳答案

遵循以下两种方法。 Here is the full tutorial link

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == 0 && resultCode == RESULT_OK && null != data) {

// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mediaPath = cursor.getString(columnIndex);
// Set the Image in ImageView for Previewing the Media
imgView.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
cursor.close();

} else {
Toast.makeText(this, "You haven't picked Image/Video", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}

}



// Uploading Image/Video
private void uploadFile() {
progressDialog.show();

// Map is used to multipart the file using okhttp3.RequestBody
File file = new File(mediaPath);

// Parsing any Media type file
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());

ApiService getResponse = ApiClient.getClient().create(ApiService.class);
Call<ServerResponse> call = getResponse.uploadFile(fileToUpload, filename);
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
ServerResponse serverResponse = response.body();
if (serverResponse != null) {
if (serverResponse.getSuccess()) {
Toast.makeText(getApplicationContext(), serverResponse.getMessage(),Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), serverResponse.getMessage(),Toast.LENGTH_SHORT).show();
}
} else {
assert serverResponse != null;
Log.v("Response", serverResponse.toString());
}
progressDialog.dismiss();
}

@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {

}
});
}

关于android - MultipartBody.Part上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51845872/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com