0

I'm able to select file, selected pdf file got uri as content://somefile but when i'm sending the file using unirest getting error file not found exception below is my code

 //file chooser
 File file;
 Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
 chooseFile.setType("*/*");
 chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
 startActivityForResult(Intent.createChooser(chooseFile, "Select a File to Upload"),1);
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == -1) {
                    file = new File(data.getData().getPath());
                    
                }

                break;
        }
    }
HttpResponse<String> response = Unirest.post("http://url/")
                            .field("file", file)
  • `file = new File(data.getData().getPath())`. getPath() does not deliver a file system path and hence you can not use the File class to access the file behind the uri. Have a look at the values of data.getData().toString() and data.getData().getPath(). No messing around with File class. You can use the uri data.getData() itself to upload the file. – blackapps Apr 02 '21 at 05:58
  • Apparently you did not use file.exist() before you called the upload code. – blackapps Apr 02 '21 at 06:00
  • I'm getting this file content:/com.android.providers.media.documents/document/document%3A32 – Siddhesh Nayak Apr 02 '21 at 09:38
  • That is a nice content scheme. Delevered with uri.toString(). Please add file.exists() in yoru code before you try to upload. Please report. – blackapps Apr 02 '21 at 09:44
  • file should exists as i was able to pick pdf file – Siddhesh Nayak Apr 02 '21 at 10:01
  • Good logic but bad programming.. Add `if (!file.exists()) return ` to your code to see. Also display a Toast() there then to inform the user. – blackapps Apr 02 '21 at 10:15
  • i added now it always returns toast file not exists any solution for that when i have selected pdf the content:/com.android.providers.media.documents/document/document%3A32 file not exists – Siddhesh Nayak Apr 02 '21 at 10:41
  • Repeat: `No messing around with File class. You can use the uri data.getData() itself to upload the file.` Well... if your library allows it. Have a look... – blackapps Apr 02 '21 at 10:54

1 Answers1

0

you have to do the typical method so please use retrofit for saving so much time

file upload example - How to Upload Image file in Retrofit 2

for networking, you have to also visit the official retrofit site https://square.github.io/retrofit/

99% of people recommend retrofit for networking and saving so much time

axar
  • 479
  • 2
  • 17
  • i'm able to make request but the new File(data.getData().getPath()) //here i'm getting content://somefile which is not found i'm able to use lib file chooser "https://github.com/hedzr/android-file-chooser" and file is uploaded ther is drawback with library cannot select pdf file – Siddhesh Nayak Apr 02 '21 at 06:47
  • `data.getData().getPath()) //here i'm getting content://somefile ` No. Not at all. getPath() never starts with content://. Further: compare with the path you get from that file chooser. – blackapps Apr 02 '21 at 09:45