0

I want the users of my app to be able to fetch a PDF document from their Drive and send it to a server.

The server-side endpoint is ready, I am opening the Drive this way:

final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
String[] mimetypes = {"application/pdf"};
chooseFileIntent.setType("*/*");     
chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
if (chooseFileIntent.resolveActivity(activity.getApplicationContext().getPackageManager()) != null) {
    chooseFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
    activity.startActivityForResult(chooseFileIntent, Uploader.PDF);
}

Then after fetching the document :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // What to do here with data?
}

This is where I'm stuck, I don't know how to handle the data in this method.

Thanks for your help.

Rob
  • 4,123
  • 3
  • 33
  • 53
  • This may help you: https://stackoverflow.com/questions/36128077/android-opening-a-file-with-action-get-content-results-into-different-uris – Shuwn Yuan Tee Jan 27 '18 at 15:30

1 Answers1

1

Android: how to fetch a document from Intent.ACTION_GET_CONTENT and transform it into an array of bytes?

That will only work for small documents. Otherwise, you will run out of memory. Use an API to connect with your server that can work with an InputStream, rather than a byte[].

I don't know how to handle the data in this method.

data.getData() in there will give you the Uri of the picked document. getContentResolver() will give you a ContentResolver, and calling openInputStream() on the ContentResolver, passing in the Uri, will give you an InputStream on the content identified by the Uri.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491