Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Intent.ATTACH_DATA

Tags:

android

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.

like image 595
Nino van Hooff Avatar asked Jun 18 '12 22:06

Nino van Hooff


People also ask

What is the use of intent createChooser () method?

Most commonly, ACTION_SEND action sends URL of build-in Browser app. While sharing the data, Intent call createChooser() method which takes Intent object and specify the title of the chooser dialog. Intent. createChooser() method allows to display the chooser.

How do you pass data between two activities through intent?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

What is intent Flag_activity_new_task?

The FLAG_ACTIVITY_NEW_TASK places your new activity on a new task stack. I'm going to refer to the activities as A and B. When you launch the first app, you have a single task with A in it. Task 1 = A. Clicking on the second activity puts B in the task.


2 Answers

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)));
like image 139
Nino van Hooff Avatar answered Nov 06 '22 17:11

Nino van Hooff


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 .

like image 22
Abhishek Garg Avatar answered Nov 06 '22 17:11

Abhishek Garg