title: android set image as contact icon/wallpaper
body: <p>I have written my own ImageViewer and now I want to have <strong>Set as</strong> functionality like in Android native ImageViewer. I now it is possible since Facebook has it. I've attached a screenshot to make myself more clear. <img src="https://i.stack.imgur.com/2nA2k.png" alt="enter image description here"></p>
  
  <p>P.S. I want to give a more detailed explanation of what goes wrong. After I choose "Contact icon" in the menu the list of my contacts appears. When I choose a contact the application force closes. If I choose "Home/Lock screen wallpaper" it opens my phone's gallery.
  Here is my code snippet:</p>
  
  <pre><code>                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"));
  </code></pre>
  
  <p>I have also added the consequent permissions to my manifest and I am able to write my image to the sd card of the phone.</p>
  
  <p><img src="https://i.stack.imgur.com/Ml7Jl.png" alt="LogCat Output"></p>