6

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.

Nino van Hooff
  • 3,677
  • 1
  • 36
  • 52

2 Answers2

14

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)));
Nino van Hooff
  • 3,677
  • 1
  • 36
  • 52
2

Above answers are great , however here is one i tested and used.

  private void setAsWallpaper(String path_of_file) {    
    try {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_ATTACH_DATA);
        File file = new File(path_of_file);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        intent.setDataAndType(FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file), getMimeType(path_of_file);
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(context, "Exception generated", Toast.LENGTH_SHORT).show();
    }
}


 private static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}

and simply call setAsWallpaper(path); here path is absolute path of file .

Abhishek Garg
  • 3,092
  • 26
  • 30