0

I made an image viewer that has 3 activities:

1) Main: where I search for files using regexp against filenames. The result is an ArrayList called 'files' that is sent using Extras to...

2) Thumbs: Through Extras it gets the filenames, and shows a grid with thumbnails (already stored locally). When you click a thumbnail, the position and 'files' is sent to...

3) Photo: It shows files(position) fullscreen.

Now, I want to make 'Photo' my default image viewer. I know how to call the class from another app. But is is possible to make 'Photo' the default viewer just by clicking an image, without calling the whole app?

Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58

1 Answers1

2

In your manifest under your "Photo" image viewer activity add the following intent filter:

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

You'll need to get the URI of the image from within the intent to display it.

dymmeh
  • 22,247
  • 5
  • 53
  • 60
  • When I click in an image, I still get `No application can perform this action` (I've disabled the Gallery app). Moreover, I got a warning on the manifest file: `Exported activity does not require permission`. – Luis A. Florit Nov 13 '12 at 18:38
  • I forgot the category.. Try the updated XML with the category added. you can set your activity to not be exported by setting 'android:exported="false"' in the tag – dymmeh Nov 13 '12 at 18:49
  • Great, this looks alive now... Just one more question, please: How do I _get the URI of the image from within the intent to display it_? I tried adding a method `public Photo(Uri uri) { ... }` inside the Photo class, without success... Thanks a lot!! – Luis A. Florit Nov 13 '12 at 18:59
  • In your activity that gets opened in the onCreate / onResume (basically anywhere in the activity) you can call Uri uri = getIntent().getData(); and that will give you the URI of the image – dymmeh Nov 13 '12 at 19:01
  • Two problems: first, the thing crashed with a `Permission denial` exception due to the lack of export. So I deleted the line, and kept the warning. But at least I get something now. Problem is that I get as uri.getPath() the following: `/external/images/media/119275`. Not even directory `/external` exists! :( But at least Photo opens without errors. Thanks for your patience... – Luis A. Florit Nov 13 '12 at 19:29
  • How are you trying to display the image? – dymmeh Nov 13 '12 at 19:38
  • With a `img.setImageBitmap(new BitmapFactory.decodeFile(filename))`, where `img` is a `TouchImageView`. The thing opens, but the problem is that the uri path is incorrect. Don't know why... – Luis A. Florit Nov 13 '12 at 19:46
  • The Uri you are provided isn't a file path Uri. It's a Uri that's to be used with content providers (databases). Trying calling img.setImageURI(uri); – dymmeh Nov 13 '12 at 20:12
  • Funny. For some images, it works. These images have uri `file://...`. Some images don't work, and those have uris like `content://media/external/images/media/119289`. Still, the folder contains only JPEGs... Maybe "My Files" app is sending wrong data to my app when I click an image?? EDIT: Mmmm... I see... Will try that. THANKS!!!!!!!!! – Luis A. Florit Nov 13 '12 at 20:15
  • BTW, to fix the warning I used this: [http://stackoverflow.com/a/12217347/1483390] – Luis A. Florit Nov 13 '12 at 23:09