After a long and winding road through the android source, I found the actual code in the default gallery application (gallery3d). I adapted for use in my own application and rewrote it again for convenience when importing to other applications. If you use or appreciate this, I ask you to cancel this answer.
Adapted from: source gallery3d in grepcode
Usage: change the first line to match the full path (starting with / mnt /) of your photo. add the string "set_as" to your strings.xml as the name of the action selection.
String absolutepath = MyApplication.appRootDir + relpath;//change for your application Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); MimeTypeMap map = MimeTypeMap.getSingleton(); String ext = absolutepath.substring(absolutepath.lastIndexOf('.') + 1); String mimeType = map.getMimeTypeFromExtension(ext); Uri uri = Uri.fromFile(new File(absolutepath)); intent.setDataAndType(uri, mimeType); intent.putExtra("mimeType", mimeType); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Activity activity = (Activity) this; activity.startActivity(Intent.createChooser( intent, activity.getString(R.string.set_as)));
source share