Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to let the user import .csv files into my app

When you want to export a .csv, I got it to work after writing this code:

Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("text/csv|text/comma-separated-values|application/csv");
String[] mimetypes = {"text/csv", "text/comma-separated-values", "application/csv"};
sendIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayListOfUris);
startActivity(Intent.createChooser(sendIntent, "Export CSV"));

When you run this code, it brings up a little chooser dialog where you can export the files into GMail, Email, Android Beam, DropBox, Google Drive, etc. And it appears to work fine with multiple files.

However, trying to accomplish the reverse (receiving multiple .csv files) has proven to be really, really hard. And I've read so many StackOverflow threads and they're either outdated or they don't actually solve the problem.

I have been trying this:

Intent receiveIntent = new Intent(Intent.ACTION_GET_CONTENT);
receiveIntent.setType("text/csv|text/comma-separated-values|application/csv");
String[] mimetypes = {"text/csv", "text/comma-separated-values", "application/csv"};
receiveIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
intent.addCategory(Intent.EXTRA_ALLOW_MULTIPLE);
startActivityForResult(Intent.createChooser(receiveIntent, "Import CSV"), REQUEST_IMPORT_CSV); //REQUEST_IMPORT_CSV is just an int representing a request code for the activity result callback later

However this only brings up a chooser that says there is no app that can complete this task, which I find baffling.

If I remove the EXTRA_ALLOW_MULTIPLE line, then I get two outcomes:

  1. If I run the app from an emulator, it goes to a file explorer where I can select a single file. As far as I can tell, that works and all, but doesn't allow me to pick multiple files.

  2. If I run the app from my physical phone, it doesn't open the file explorer -- it gives me the option to import from DropBox or some other app that isn't really relevant.

Why isn't it allowing the file explorer? Why isn't it allowing other options like Google Drive for example? Is there really no service for importing multiple files into your app? Does it only give you the file explorer when it can't find any other app to invoke?

My end goal here is to basically prompt the user with a chooser that lets them pick one or more .csv files from either an external app like DropBox, or from their own file system via a file explorer.

Or is my code wrong somehow?

What do I do? The Android documentation offers no help on the matter.

Edit: My emulator phone is a Galaxy Nexus API 23 and a Nexus 5 API 22. My physical phone is a Samsung Galaxy Note 4 (version 6.0.1).

like image 656
user7017484 Avatar asked Oct 16 '25 20:10

user7017484


1 Answers

The following works for me

receiveIntent.setType("*/*");
String[] mimetypes = {"text/csv", "text/comma-separated-values", "application/csv"};
receiveIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
like image 175
EmmanuelMA Avatar answered Oct 18 '25 10:10

EmmanuelMA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!