3

I want to upload pdf to my server.For this i want to allow user to choose file from their local device as well as google drive

If user select file from their device then i get uri like content://com.android.providers.downloads.documents/document/5447 but when user select file from their google drive then i get uri like content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D10324 and i am unable to convert this uri to file

Code for choosing file

  private void showFileChooser() {
    Intent intent = new Intent();    
    intent.setType("application/pdf");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivityForResult(Intent.createChooser(intent,"Choose File to Upload.."),PICK_FILE_REQUEST);
}

Code for creating multipart

public MultipartBody.Part GetMultiPartCV(Uri uri) {

    if (uri!= null) {
        File file = FileUtils.getFile(this, uri);

        RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(uri)), file);

        return MultipartBody.Part.createFormData("file", file.getPath(), requestFile);
    } else {
        return null;
    }
}

When i upload from downloaded folder it works perfectly because i am able to convert uri to file but when i choose filr from google drive i am unable to convert uri to file so it gives me null pointer exception .

Rajat
  • 763
  • 11
  • 17

0 Answers0