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. 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 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.@Overridepublic 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.@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent pickedimage) {if (resultCode == Activity.RESULT_OK) {String imageid = pickedimage.getData().getLastPathSegment();//get titleCursor 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 thumbnailBitmap thumbnail = Thumbnails.getThumbnail(getContentResolver(),Integer.parseInt(imageid),Thumbnails.MICRO_KIND, null);//when the shortcut is clicked, view the selected imageIntent dataview = new Intent();dataview.setData(pickedimage.getData());dataview.setAction(Intent.ACTION_VIEW);dataview.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);//create shortcut and return itIntent 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();}}
|