1569days since
ADC1 announcement

1224days since
Device availability

Components‎ > ‎

Image Shortcut

Image Shortcut allows you to create a homescreen/launcher shortcut to a specific image in the gallery of your phone.
The shortcut shows the thumbnail and title of your image. Clicking the shortcut opens the gallery on the selected image.

Any app (including most homescreen apps) can request a list of installed apps that declare an activity with the create_shortcut action filter.
When Image Shortcut is selected from the list, the declared activity is started where is decided what the shortcuts title, icon and intent (when clicked on) should be. The shortcut is then returned to the requesting app which makes the it appropriately accessible for the user to click on.
For both picking an image to shortcut to and when actually opening an image, Image Shortcut relies on an other app (such as the default gallery app) to handle the pick and view intents.


protip: your app needs to have a launchable activity in the manifest to be able to debug it 

To-Do

Even though the image picking intent should open the content provider of the mediastore's images, the picked image intent returned in onactivityresult could refer to non-image media content. (video)


package arnodenhond.imageshortcut;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Images.Thumbnails;
import android.widget.Toast;

public class ImageShortcut extends Activity {

//created when user chooses to add one of our shortcuts. starts the imagepicker intent.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, R.string.selectimage, Toast.LENGTH_SHORT).show();
Intent pickimage = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickimage, Activity.RESULT_FIRST_USER);
}
//receives the selected image. creates shortcut and returns it.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent pickedimage) {
if (resultCode == Activity.RESULT_OK) {
String imageid = pickedimage.getData().getLastPathSegment();
//get title
Cursor cursor = managedQuery(Media.EXTERNAL_CONTENT_URI,
new String[] { Media._ID, Media.TITLE }, Media._ID + "=?",
new String[] { imageid }, null);
cursor.moveToFirst();
String title = cursor.getString(1);
cursor.close();
//get thumbnail
Bitmap thumbnail = Thumbnails.getThumbnail(getContentResolver(),
Integer.parseInt(imageid),
Thumbnails.MICRO_KIND, null);
//when the shortcut is clicked, view the selected image
Intent dataview = new Intent();
dataview.setData(pickedimage.getData());
dataview.setAction(Intent.ACTION_VIEW);
dataview.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

//create shortcut and return it
Intent result = new Intent();
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, dataview);
result.putExtra(Intent.EXTRA_SHORTCUT_ICON, thumbnail);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
setResult(RESULT_OK, result);
Toast.makeText(this, R.string.shortcutadded, Toast.LENGTH_SHORT).show();
} else {
setResult(RESULT_CANCELED);
Toast.makeText(this, R.string.shortcutcanceled, Toast.LENGTH_SHORT)
.show();
}
finish();
}

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="arnodenhond.imageshortcut" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />
<application android:icon="@drawable/imageshortcut"
android:label="@string/imageshortcut">
<activity android:name=".ImageShortcut" android:label="@string/imageshortcut">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>
</application>
</manifest>