gpt4 book ai didi

android - 打开失败 : ENOENT (No such file or directory) Unable to get correct URI format

转载 作者:行者123 更新时间:2023-11-29 19:38:58 25 4
gpt4 key购买 nike

我正在使用提供的说明尝试 Retrofit POST here .该帖子推荐使用非常旧的文件选择器,如下所示。

//https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
// use the FileUtils to get the actual file by uri
File file = FileUtils.getFile(this, fileUri);

取而代之的是,我决定使用 intent 使用下面的代码获取文件。

uploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK
&& data != null && data.getData() != null) {
Uri fileUri = data.getData();

Log.d(LOG_TAG, "fileUri " + fileUri.toString());

try {
File file = new File(fileUri.getPath());
Log.d(LOG_TAG, "file " + file.toString());
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file.getAbsoluteFile());

// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

// add another part within the multipart request
String descriptionString = "POINT(12.9085608 77.6106535)";

RequestBody location =
RequestBody.create(
MediaType.parse("multipart/form-data"), descriptionString);

// finally, execute the request
Call<ResponseBody> call = mAbService.uploadImage(location, body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call,
Response<ResponseBody> response) {
Log.v(LOG_TAG, "success");
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(LOG_TAG, t.getMessage());
}
});
} catch (Exception e) {
Log.d(LOG_TAG, "file failed");
}
}

我无法获得正确的路径,无法将文件上传到我的服务器。

08-10 01:28:36.564 20093-20093/com.sudhirkhanger.app.test D/TestActivityFragment: fileUri content://com.android.providers.media.documents/document/image%3A57373
08-10 01:28:36.564 20093-20093/com.sudhirkhanger.app.test D/TestActivityFragment: file /document/image:57373
08-10 01:28:36.658 20093-20093/com.sudhirkhanger.app.test E/TestActivityFragment: /document/image:57373: open failed: ENOENT (No such file or directory)

最佳答案

ACTION_GET_CONTENT 不返回文件。它返回指向一段内容的 Uri。该内容不一定是文件,更不用说您可以访问的文件了。 File file = new File(fileUri.getPath()); 没用。

我没有使用过 OkHttp3/Retrofit 的 RequestBody。据我所知,您需要 create your own implementation that can work off of an InputStream .您可以通过在 ContentResolver 上调用 openInputStream() 来获取 InputStream,您可以通过调用 getContentResolver() 来获取该 InputStreamContext 上。

关于android - 打开失败 : ENOENT (No such file or directory) Unable to get correct URI format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38859571/

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