1

I want csv files to be opended by my app on android. I put in the following code into my AndroidManifest, but nothing happens when I install it on my Android simulator or my phone and try to open a .csv file from my storage space.

    <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="file" />
            <data android:mimeType="*/*" />
            <data android:pathPattern=".*\\.csv" />
            <data android:host="*" />
        </intent-filter>
  1. What am I missing that stops me from opening .csv files from my phone?
  2. How do I pass the file url to my app so it knows where to access it?
Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

2

What am I missing that stops me from opening .csv files from my phone?

Your intent filter needs following changes

  • Use mimeType filter instead of pathPattern, pathPattern is very difficult to get right with all kinds of possible file names, since you want to open csv file it is best to use mimeType filter "text/csv" should do

  • Change scheme to "content", scheme file does not work post Android 7

Android 7.0 Behavior Changes

Sharing Files Between Apps For apps targeting Android 7.0, the Android framework enforces the StrictMode API policy that prohibits exposing file:// URIs outside your app. If an intent containing a file URI leaves your app, the app fails with a FileUriExposedException exception.

To share files between applications, you should send a content:// URI and grant a temporary access permission on the URI. The easiest way to grant this permission is by using the FileProvider class. For more information on permissions and sharing files, see Sharing Files.

Change your intent-filter to following, it should work.

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="content" />
            <data android:mimeType="text/csv"/>
            <data android:host="*" />
        </intent-filter>
Valeo T
  • 66
  • 5
  • This WORKED! Thanks alot. A follow up question if I may: how do I get the Uri of the file that is opened, so my app can access it? – user8964609 Jan 29 '18 at 04:14
0

Thank you for the above response. It worked!

Now, to access the file from the app: https://richardleggett.com/blog/2013/01/26/registering_for_file_types_in_android/

In short, in the onCreate(Bundle savedInstanceState) method of the main file use this:

Uri uri= getIntent().getData();

Once you have the URI, you can access the file with your usual code:

BufferedReader br = new BufferedReader(new 
InputStreamReader(getContentResolver().openInputStream(uri), "UTF-8"));

Then use the BufferedReader to read each line with:

String strLine = br.readLine();

Hope this helps others!

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
0

This code is work for me:

        !-- default intent filter for launching activity -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <!-- example filter to retrieve or view plain text -->
        <!-- adds a custom title and icon for the share option -->
        <intent-filter
            android:icon="@mipmap/icon3"
            android:label="@string/app_name">

            <data android:mimeType="text/csv" />
            <data android:mimeType="text/comma-separated-values" />

            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
Dmitry Grushin
  • 701
  • 5
  • 6