Android image set as contact icon / wallpaper

I wrote my own ImageViewer, and now I want to have Install as functionality, for example, in Android's own ImageViewer. Now I can do it, since Facebook has it. I have attached a screenshot to make myself clearer. enter image description here

PS I want to give a more detailed explanation of what goes wrong. After I select "Contact Icon" in the menu, a list of my contacts will appear. When I select a contact, the power of the application closes. If I select “Desktop / Lock Screen”, it will open my phone gallery. Here is my code snippet:

Bitmap icon = mBitmap; Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA); setAs.setType("image/jpg"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes); File f = new File(Environment.getExternalStorageDirectory() + File.separator + "/my_tmp_file.jpg"); try { f.createNewFile(); FileOutputStream fo = new FileOutputStream(f); fo.write(bytes.toByteArray()); } catch (IOException e) { e.printStackTrace(); } setAs.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/my_tmp_file.jpg")); startActivity(Intent.createChooser(setAs, "Set Image As")); 

I also added the appropriate permissions for my manifest, and I can write my image to the phone’s SD card.

Logcat output

+6
source share
5 answers

From the source code of the Google Gallery app :

 // Called when "Set as" is clicked. private static boolean onSetAsClicked(MenuInvoker onInvoke, final Activity activity) { onInvoke.run(new MenuCallback() { public void run(Uri u, IImage image) { if (u == null || image == null) { return; } Intent intent = Util.createSetAsIntent(image); activity.startActivity(Intent.createChooser(intent, activity.getText(R.string.setImage))); } }); return true; } 

From Utils.java

 // Returns an intent which is used for "set as" menu items. public static Intent createSetAsIntent(IImage image) { Uri u = image.fullSizeImageUri(); Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); intent.setDataAndType(u, image.getMimeType()); intent.putExtra("mimeType", image.getMimeType()); return intent; } 
+4
source

Take a look at the contacts app code. There is an AttachImage function that runs to attach an image. The photo of the icon should be 96x96 px in size. action...CROP recognize and crop the image you are transmitting.

Link: AttachImage.java

You must scale and crop the image to 96x96 and pass its URI to the insertPhoto method used in AttachImage . To change the wallpaper you can turn to this question .

Update

Code for starting work on agriculture:

 Intent intent = new Intent("com.android.camera.action.CROP", myIntent.getData()); if (myIntent.getStringExtra("mimeType") != null) { intent.setDataAndType(myIntent.getData(), myIntent.getStringExtra("mimeType")); } intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 96); intent.putExtra("outputY", 96); intent.putExtra("return-data", true); startActivityForResult(intent, REQUEST_CROP_PHOTO); 
+3
source

You can simply use WallpaperManager to set the wallpaper.

 WallpaperManager.getInstance(this).setBitmap(mBitmap); 
+1
source

To set the image as (contact, wallpaper, etc.)

  Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA); setAs.setType("image/jpg"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); File f = new File(Environment.getExternalStorageDirectory() + File.separator + "/my_tmp_file.jpg"); try { f.createNewFile(); FileOutputStream fo = new FileOutputStream(f); fo.write(bytes.toByteArray()); } catch (IOException e) { e.printStackTrace(); } setAs.setDataAndType(Uri.parse("file:///sdcard/my_tmp_file.jpg"), "image/jpg"); setAs.putExtra("mimeType", "image/jpg"); startActivity(Intent.createChooser(setAs, "Set Image As")); 

This will solve your problem and set the image as (contact, wallpaper, etc.)

0
source

use this code

 File externalFile=new File("filePath"); Uri sendUri = Uri.fromFile(externalFile); Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); intent.setDataAndType(sendUri, "image/jpg"); intent.putExtra("mimeType", "image/jpg"); startActivityForResult(Intent.createChooser(intent, "Set As"), 200); 
0
source

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


All Articles