Search code examples
androidandroid-intentfile-typefile-browser

Android FileType viewer


Alright, so I have an app that has a custom folder:

I want to open the following types:

protected String[] acceptableTypes = {"jpg", "gif", "png", "bmp", "pdf", "txt"};

I can open the images or at least the jgps for sure with the following code whenever I click on the file:

MediaScannerConnection.scanFile(((Activity) getContext()), new String[] { filePath }, null, 
                new MediaScannerConnection.OnScanCompletedListener() { 
            @Override public void onScanCompleted(String path, Uri uri) { 
                Intent intent = new Intent(Intent.ACTION_VIEW); 
                intent.setDataAndType(uri, "image/*"); 
                ((Activity) getContext()).startActivityForResult(intent, ConferenceActivity.ACTIVITY_SELECT_IMAGE); 
            } 
        });

Now how can I filter this so that I can open the correct file type with the correct program?


Solution

  • Read http://developer.android.com/guide/components/intents-filters.html for an intro on how to do this. Search from "image/" on that page.

    You need to declare the file types in your application's manifest file. Be sure and declare them using a mimeType like "image/*" or "image/png" so that you can open the files from other applications like gmail.

    In your activity you would add something like this:

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <data android:mimeType="image/*" />
    </intent-filter>
    

    The other catch is that you also have to handle both "file" schemes and "content" schemes. To see what I mean open a file from Gmail in Jellybean and you will see that the URI you are provided is a content type instead of the typical file type.