How to use Intent.ATTACH_DATA

I am trying to implement a set as function for images. I use Intent.ATTACH_DATA so that users can at least select a contact photo and wallpaper. Additional things that I had to bring down confused me. If I read the documentation correctly,

Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); intent.setType("image/*"); intent.setData(mImageCaptureUri); startActivity(Intent.createChooser(intent, "hey")); 

That should be all. This works for wallpapers, but with megapixel data, the application crashes due to the lack of activity in the plant. Does anyone have a working example? The official gallery app manages to find the camera.crop function ...

A generally accepted hint about where to find detailed documentation of the intentions of the system is also welcome.

+6
source share
1 answer

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))); 
+13
source

Source: https://habr.com/ru/post/918417/


All Articles