open failed:ENOENT(没有这样的文件或目录)无法获得正确的URI格式

时间:2016-08-09 20:10:04

标签: android retrofit2

我正在尝试使用here提供的说明进行Retrofit POST。帖子推荐使用和非常旧的文件选择器如下。

//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) 

1 个答案:

答案 0 :(得分:2)

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

我没有使用OkHttp3 / Retrofit&#39; RequestBody。从我所看到的,你需要create your own implementation that can work off of an InputStream。您可以通过InputStream致电openInputStream()来获得ContentResolver getContentResolver(),您可以通过Context致电<name>Crazedo</name> <description>Makes Human Crazy</description> <author email="adtsalvation@gmail.com" href="https://crazedo.com">ADT Salvation</author> <preference name='phonegap-version' value='5.0.0' /> <feature name="http://api.phonegap.com/1.0/device" /> <preference name="orientation" value="portrait" /> <!-- all: default means both landscape and portrait are enabled --> <gap:plugin name="org.apache.cordova.device" /> <gap:plugin name="org.apache.cordova.camera" /> <gap:plugin name="org.apache.cordova.media-capture" /> <gap:plugin name="org.apache.cordova.file" /> <gap:plugin name="org.apache.cordova.file-transfer" /> <gap:plugin name="org.apache.cordova.splashscreen" /> <gap:plugin name="org.apache.cordova.contacts" /> <gap:plugin name="org.apache.cordova.geolocation" /> <gap:plugin name="org.apache.cordova.inappbrowser" /> <gap:plugin name="org.apache.cordova.dialogs" /> <gap:plugin name="org.apache.cordova.vibration" /> <gap:plugin name="org.apache.cordova.network-information" /> <gap:plugin name="com.simplec.plugins.localnotification" /> <icon src="icon.png" /> <icon src="res/icons/ios/Icon.png" gap:platform="ios" width="57" height="57" /> <icon src="res/icons/ios/Icon@2x.png" gap:platform="ios" width="114" height="114" /> <icon src="res/icons/ios/Icon-72.png" gap:platform="ios" width="72" height="72" /> <icon src="res/icons/ios/Icon-72@2x.png" gap:platform="ios" width="144" height="144" /> <icon src="res/icons/android/drawable-ldpi/Icon.png" gap:platform="android" gap:density="ldpi" /> <icon src="res/icons/android/drawable-mdpi/Icon.png" gap:platform="android" gap:density="mdpi" /> <icon src="res/icons/android/drawable-hdpi/Icon.png" gap:platform="android" gap:density="hdpi" /> <icon src="res/icons/android/drawable-xdpi/Icon.png" gap:platform="android" gap:density="xhdpi" /> <gap:splash src="splash.png" /> <gap:splash src="res/screen/android/drawable-ldpi/splash.png" gap:platform="android" gap:density="ldpi" /> <gap:splash src="res/screen/android/drawable-mdpi/splash.png" gap:platform="android" gap:density="mdpi" /> <gap:splash src="res/screen/android/drawable-hdpi/splash.png" gap:platform="android" gap:density="hdpi" /> <gap:splash src="res/screen/android/drawable-xdpi/splash.png" gap:platform="android" gap:density="xhdpi" /> <gap:splash src="res/screen/ios/Default.png" gap:platform="ios" width="320" height="480" /> <gap:splash src="res/screen/ios/Default@2x.png" gap:platform="ios" width="680" height="960" /> <gap:splash src="res/screen/ios/Deafult-568h-2x.png" gap:platform="ios" width="640" height="1136" /> <content src="https://crazedo.com" /> <access origin="*" subdomains="true" /> <allow-navigation href="*" /> 来获取。{/ p>