3

I have written a CSV programmatically, I want to allow the user to be able to open the CSV in an app that they have installed on their device.

I have the following:

File file = new File(Environment.getExternalStorageDirectory(), "My_App_Dir/" + fileName);
Intent csvIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
csvIntent.addCategory(Intent.CATEGORY_OPENABLE);
csvIntent.setDataAndType(FileProvider.getUriForFile(getActivity(), getActivity().getApplicationContext().getPackageName() + ".provider", finalFile), "text/csv");
csvIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
csvIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(csvIntent);

When I run the above code, all I get is the recent files explorer (which doesn't show the file) not the app selection screen to allow the user to select which application they want to use to open the file.

If I go to my devices file explorer app (solid explorer) and select the app, it provides me a list of apps that are capable of supporting the CSV (Google Docs or Excel).

1 Answer 1

3

Use ACTION_VIEW. ACTION_OPEN_DOCUMENT is the equivalent of a desktop OS or Web browser "file open" dialog.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I've given that a try but it throws an ActivityNotFoundException now. android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.OPENABLE] dat=content://com.my_company.my_app.provider/external_files/My_App_Dir/11042017_233342_resultset.csv typ=text/csv flg=0x10000001 }
Ah sorted it, done what you did, but I needed to remove the addCategory(Intent.CATEGORY_OPENABLE). Thanks for your help
@Boardy: Oh, yeah, right, sorry about missing that. ACTION_VIEW is usually only used with the default category, and that gets added by default when you call startActivity().
This is not available in API 15. Do you have any other suggestion?
@tiagocarvalho92: I do not know what "this" is. ACTION_VIEW -- which I recommended in my answer, has been around since API Level 1.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.