title: Android Copy Image to clipboard/clipdata
body: <p>Long press on images in the web or at other places gives me the opportunity to copy an image to the clipboard of my device. See here:</p>
  
  <p><a href="https://i.stack.imgur.com/8fTtt.png" rel="noreferrer"><img src="https://i.stack.imgur.com/8fTtt.png" alt="Example"></a></p>
  
  <p>Now I want to implement this into my app. What I have so far:</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>Bitmap bitmap = getBitmap();
          File file = storeImage(bitmap, name);
  
          //Share
          Intent shareIntent = new Intent(Intent.ACTION_SEND);
          shareIntent.setType("image/*");
          Uri uri = Uri.fromFile(file);
          shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
  
          //Add Copy to Clipboard to choosers
          Intent clipboard = new Intent(this, CopyToClipboardImageActivity.class);
          clipboard.setData(uri);
  
          Intent chooserIntent = Intent.createChooser(shareIntent, getString(R.string.shareScreenshot));
          chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{clipboard});
          startActivity(chooserIntent);
  </code></pre>
  
  <p><strong>CopyToClipboardImageActivity</strong></p>
  
  <pre><code>  public class CopyToClipboardImageActivity extends Activity {
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
  
              Uri uri = getIntent().getData();
              if (uri != null) {
                  copyImageToClipboard(uri);
                  Toast.makeText(this, getString(R.string.hinweisInZwischenablageKopiert), Toast.LENGTH_SHORT).show();
              }
  
              // Finish right away. We don't want to actually display a UI.
              finish();
          }
  
          private void copyImageToClipboard(Uri uri) {
              ClipboardManager mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
              ContentValues values = new ContentValues(2);
              values.put(MediaStore.Images.Media.MIME_TYPE, "Image/jpg");
              values.put(MediaStore.Images.Media.DATA,  "file://"+uri.getPath());
  
              ContentResolver theContent = getContentResolver();
              Uri imageUri = theContent.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
              ClipData theClip = ClipData.newUri(getContentResolver(), "Image", imageUri);
              mClipboard.setPrimaryClip(theClip);
          }
      }
  </code></pre>
  
  <p>But this does not work yet. Either it only copies a weird path to the clipboard (See above picture^^) or I get the following NullpointerException:</p>
  
  <p><em>Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference</em></p>
  
  <p>in this line  <code>ClipData theClip = ClipData.newUri(getContentResolver(), "Image", imageUri);</code></p>