Rob*_*scu 3 java android photo file multipart
我有一个 Android 应用程序,需要让用户从图库中选择一些图片并将这些图片发送到后端(连同一些其他数据)。
为了允许用户选择图片,我的 Fragment 中有以下内容:
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);
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到用户所选照片的结果:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将 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);
Run Code Online (Sandbox Code Playgroud)
问题是这有时有效,有时无效。有时我会收到错误“java.io.FileNotFoundException”,这会让我陷入onFailure()Retrofit 调用的回调中。
我发现以下 stackoverflow 帖子从 Uri 读取文件给出了 java.io.FileNotFoundException: open failed: ENOENT但我不确定如何在对我的特定情况的响应中实施一般建议。
获得用户选择的图片的正确路径的正确方法是什么,以便我可以从中创建文件并将它们附加到我的 MultiPart 请求中?
Commonsware 建议
使用 ContentResolver 和 openInputStream() 在 Uri 指向的内容上获取 InputStream。然后,将其传递给您的解码逻辑,例如 BitmapFactory 及其 decodeStream() 方法。
,但我不确定如何以编程方式做到这一点。
任何帮助,将不胜感激。
为了允许用户选择图片,我的 Fragment 中有以下内容:
此代码正在使用ACTION_GET_CONTENT. 特别是在 Android 7.0+ 上,通常会返回Uri带有content方案的值。您的代码假定您正在Uri通过一个file方案获取值,其中路径实际上具有意义。此外,您的代码假定用户正在选择您可以访问的文件系统上的文件,并且没有什么可以强制用户这样做。ACTION_GET_CONTENT可以由其内容为的应用程序支持:
BLOB数据库列中的字节流而不是使用RequestBody.create(),使用InputStreamRequestBody来自这个 OkHttp 问题评论。您提供与以前相同的媒体类型,但不是 a File(您错误地创建),而是提供 a ContentResolver(来自getContentResolver()a Context)和Uri.
这篇博文演示了如何使用InputStreamRequestBody(特别是原始的 Kotlin 端口)以这种方式上传内容。这篇博文提供了对同一问题和类似解决方案的另一种看法。
| 归档时间: |
|
| 查看次数: |
2568 次 |
| 最近记录: |