0

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?

Vnge
  • 1,295
  • 25
  • 49

2 Answers2

0

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.

Nathan
  • 1,747
  • 1
  • 12
  • 12
  • The snippets of code are from a bigger app. So, the files will only need to be opened from with in this app – Vnge Jan 27 '13 at 21:18
  • You can't do that exactly. You could provide a "pathPattern" field in your intent filter to prevent your app from opening other images from other places. But if the user touches a .jpeg they get all the .jpeg apps and you can't stop that. – Nathan Jan 27 '13 at 21:21
  • Same rules for every file type. Now if you are in your own activity you can choose to open the file however you want. You would explicitly start the activity that you want. startActivity( new Intent(this, PngActivity.class) ); – Nathan Jan 27 '13 at 21:27
  • My goal is to use the default text viewer from when the user clicks on the file. – Vnge Jan 27 '13 at 21:31
  • use the txt file's mimeType (text/plain i think) when you create the intent. And add the txt files path to the intent by it's URI. Just google for how to do that. – Nathan Jan 27 '13 at 21:37
0

you just have to pass the right mimeType in setDataAndType. You can get the mimeType of a file using URLConnection.guessContentTypeFromName

So something like this:

Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
String mimeType = URLConnection.guessContentTypeFromName(filePath);
i.setDataAndType(Uri.fromFile(new File(filePath)), mimeType);
startActivity(i);
J-L
  • 189
  • 1
  • 8