Launch with an intent

Wouldn't it be cool if you could launch this app any time you go to view an image on your phone, especially 360-degree photospheres?

One of the more powerful features of the Android operating system is the ability to communicate between apps with intents. An intent is a message that any app can send to the Android system, which declares its intent to use another app for a certain purpose. The intent object contains a number of members to describe what type of action needs to be done, and, if any, the data on which it needs to be done. As a user, you may be familiar with the default action picker, which displays a number of app icons, and the choices, Just Once, or Always. What you're seeing is the result of the app you were just using broadcasting a new intent to the system. When you choose an app, and Android launches a new activity from that app, which has been registered to respond to intents of that type.

In your AndroidManifest.xml file, add an intent filter to the activity block. Let Android know that the app can be used as an image viewer. Add the following XML code:

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

We just need to handle the situation so an intent image is the default image loaded when the app starts. In MainActivity, we'll write a new function that shows an image given its URI, as follows. The method gets the URI path and translates it into a file pathname, calls the new Image object on that path, and then the showImage method. (For reference, visit http://developer.android.com/guide/topics/providers/content-provider-basics.html):

    void showUriImage(final Uri uri) {
        Log.d(TAG, "intent data " + uri.getPath());
        File file = new File(uri.getPath());
        if(file.exists()){
            Image img = new Image(uri.getPath());
            showImage(img);
        } else {
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
            if (cursor == null)
                return;
            if (cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String yourRealPath = cursor.getString(columnIndex);
                Image img = new Image(yourRealPath);
                showImage(img);
           }
           // else report image not found error?
           cursor.close();
       
    }

Then, add a call to showUriImage from setup, as follows:

    public void setup() {
        BorderMaterial.destroy();
        setupMaxTextureSize();
        setupBackground();
        setupScreen();
        loadImageList(imagesPath);
        setupThumbnailGrid();
        setupScrollButtons();
        Uri intentUri = getIntent().getData();
        if (intentUri != null) {
            showUriImage(intentUri);
        }
        updateThumbnails();
    }

We've also added a call to BorderMaterial.destroy() since the intent launches a second instance of the activity. If we don't destroy the materials, the new activity instance, which has its own graphics context, will throw errors when it tries to use shaders compiled on the first activity's graphics context.

Now with the project built and installed on the phone, when you choose an image file, for example, from a file folder browser app such as My Files (Samsung), you're given a choice of apps with an intent to view images. Your Gallery360 app (or whatever you have actually named it) will be one of the choices, as shown in the following screenshot. Pick it and it will launch with that image file view as the default.

Launch with an intent
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset