How to use Intent.ATTACH_DATA

android

Solution

After a long and winding road through the android source, I found the actual code in the default gallery (gallery3d) app. I adapted for use in my own application, and rewrote it again for convenience when importing in other applications. If you use or appreciate this, I ask that you upvote this answer.

Adapted from : gallery3d source at grepcode

Usage: change first line to match the full path (starting with /mnt/) of your photo. add string "set_as" to your strings.xml as the action chooser title.

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)));

Problem

I'm trying to implement "set as" functionality for images. I'm using Intent.ATTACH_DATA so users can at least choose contact photo and wallpaper. The extras I should pass confuse me. If I read the documentation right, ``` Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); intent.setType("image/*"); intent.setData(mImageCaptureUri); startActivity(Intent.createChooser(intent, "hey")); ``` Should be all. This works for wallpapers, but with megapixel data, the app crashes, because no crop activity could be found. Does someone have a working example? The official gallery app does manage to find the camera.crop activity... A general hint on where to find elaborate system intent documentation is welcome as well.

Original source