【发布时间】:2021-12-30 23:18:00
【问题描述】:
我有一个 Android 应用程序需要让用户从图库中选择一些图片并将这些图片发送到后端(连同一些其他数据)。
为了允许用户选择图片,我的片段中有以下内容:
private void pickImages() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}
我在这里得到用户选择照片的结果:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
Toast.makeText(getActivity(), "There was an error getting the pictures", Toast.LENGTH_LONG).show();
return;
}
ClipData clipData = data.getClipData();
String fileName = null, extension = null;
//if ClipData is null, then we have a regular file
if (clipData == null) {
//get the selected file uri
fileName = FileUtils.getPath(getActivity(), data.getData());
//obtain the extension of the file
int index = fileName.lastIndexOf('.');
if (index > 0) {
extension = fileName.substring(index + 1);
if (extension.equals("jpg") || extension.equals("png") || extension.equals("bmp") || extension.equals("jpeg"))
isAttachedFile = true;
}
}
ArrayList<Uri> photosUris = new ArrayList<>();
//for each image in the list of images, add it to the filesUris
if (clipData != null) for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
Uri uri = item.getUri();
switch (i) {
case 0:
picture1Uri = uri;
break;
case 1:
picture2Uri = uri;
break;
}
photosUris.add(uri);
}
else if (isAttachedFile) {
Uri uri = Uri.parse(fileName);
picture1Uri = uri;
photosUris.add(uri);
}
uris = photosUris;
if (picture1Uri != null) {
image1.setVisibility(View.VISIBLE);
image1.setImageURI(picture1Uri);
}
if (picture2Uri != null) {
image2.setVisibility(View.VISIBLE);
image2.setImageURI(picture2Uri);
}
}
然后,我将 URI 列表发送给 Presenter,在那里我执行对后端的 MultiPart Retrofit 调用:
//obtain the file(s) information of the message, if any
if (uris != null && uris.size() > 0) {
for (int i = 0; i < uris.size(); i++) {
File file = null;
//this is the corect way to encode the pictures
String encodedPath = uris.get(i).getEncodedPath();
file = new File(encodedPath);
builder.addFormDataPart("photos[]", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
}
}
MultipartBody requestBody = builder.build();
//send the newly generated ticket
Call<GenerateNewTicketResponse> generateNewTicketCall = OperatorApplication.getApiClient().generateNewTicket(Constants.BEARER + accessToken, requestBody);
问题是这有时有效,有时无效。有时我会收到错误“java.io.FileNotFoundException”,这会将我抛出到 Retrofit 调用的 onFailure() 回调中。
我发现了以下 stackoverflow 帖子 Reading File from Uri gives java.io.FileNotFoundException: open failed: ENOENT,但我不确定如何在针对我的特定情况的响应中实施一般建议。
获取用户选择的图片的正确路径以便我可以从中创建文件并将它们附加到我的 MultiPart 请求中的正确方法是什么?
普通软件建议
使用 ContentResolver 和 openInputStream() 在 Uri 指向的内容上获取 InputStream。然后,将其传递给您的解码逻辑,例如 BitmapFactory 及其 decodeStream() 方法。
,但我不确定如何以编程方式执行此操作。
任何帮助将不胜感激。
【问题讨论】:
标签: java android file photo multipart