处理请求时捕获到java.io.FileNotFoundException:/document/raw:/storage/emulated/0/Download/mypdf.pdf(没有这样的文件或目录)

Tej*_*dya 6 pdf android filepath android-contentprovider

我想使用multipart请求将pdf发送到我的服务器.我能够正确选择文件并得到它的名字但是当我发送这个pdf时,我发送以下路径/document/raw:/storage/emulated/0/Download/kitchenapp.pdf.路径是正确的,文件在那里,但我有这个例外.I/DefaultRequestDirector: I/O exception (java.io.FileNotFoundException) caught when processing request: /document/raw:/storage/emulated/0/Download/kitchenapp.pdf (No such file or directory)

到目前为止我做了什么..通过这个获取pdf

 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("application/pdf");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                1);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(getActivity(), "Please install a File Manager.",
                Toast.LENGTH_SHORT).show();
    }
Run Code Online (Sandbox Code Playgroud)

通过此获取onActivity结果

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri selectedFileURI = data.getData();
         file = new File(selectedFileURI.getPath().toString());
        Log.d("", "File : " + file.getName());
        String uploadedFileName = file.getName().toString();
        System.out.println("upload file name "+uploadedFileName);

        System.out.println("my location "+file);



    }
}
Run Code Online (Sandbox Code Playgroud)

通过多部分请求发送此文件

  if (file != null ) {


                entity.addPart("file", new FileBody(file));
            }

            // totalSize = entity.getContentLength();
            httppost.setEntity(entity);

            // Making server call
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity r_entity = response.getEntity();
Run Code Online (Sandbox Code Playgroud)

任何帮助将是欣赏..

Bri*_*off 7

我刚刚从这个精彩的答案修复了NumberFormat代码片段中的崩溃.你可以在这里找到我的答案代码.