Search code examples
androidpdfandroid-contentproviderfilepath

java.io.FileNotFoundException caught when processing request: /document/raw:/storage/emulated/0/Download/mypdf.pdf (No such file or directory)


I want to send pdf to my server using multipart request. i am able to choose file correctly and get its name but when i am sending this pdf , i am sending following path /document/raw:/storage/emulated/0/Download/kitchenapp.pdf . path is correct and file is there , yet i got this exception . 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)

what i have done so far .. Get pdf by this

 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();
    }

get onActivity result by this

@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);



    }
}

sending this file via multipart request

  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();

any help would be appreciate..


Solution

  • I've just fixed a NumberFormat crash in code snippet from this brilliant answer. You can find my answer with code here.