2

So I have been through the posts on stackoverflow and there is no real mention of how to copy actual image data to a clipboard. I can copy the uri to the clipboard like so:

  ContentValues values = new ContentValues(2);
  values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
  values.put(MediaStore.Images.Media.DATA, android.os.Environment.getExternalStorageDirectory() + path);
  ContentResolver theContent = getContentResolver();
  Uri  imageUri = theContent.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  
  ClipData theClip = ClipData.newUri(getContentResolver(),"Image", imageUri);
  ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
  clipboard.setPrimaryClip(theClip);

This lets me paste the uri afterwards, which is pretty worthless in most contexts unless you control the activity.

I can also send the image data to the intent directly like so:

  Intent sendIntent = new Intent(Intent.ACTION_SEND); 
  sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
  sendIntent.putExtra("thread_id", (long) 1);
  sendIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  sendIntent.setType("image/*"); 
  startActivity(sendIntent);

While this is useful, I can clearly see that pasting images to the clipboard is possible by going to the gallery and selecting "copy to clipboard" in the dropdown. This lets me paste said image to the messenger, email, chat, etc. The same seems to also be possible on most Android browsers starting 4.4.2. Is there any working code that anyone can point me to or pointers to get image data actually copied to the keyboard and not just the uri? I can see that it's possible and I'm kind of running out of ideas at this point.

Here is a valid example of a uri I am using: "content://media/external/images/media/63"

HarshMarshmallow
  • 1,007
  • 11
  • 23
  • Just a thought, but those applications you paste the image in could be reading the file at the location of the URI. – Mapsy Jan 29 '15 at 16:45
  • I kind of figured that was the case. Don't really have any way to figure out what's added to the uri so that those applications view the link as an image. I guess I could look at the clipboard contents after something is copied correctly and see what's there. Was hoping this little project would be easier than that – HarshMarshmallow Jan 29 '15 at 17:28
  • Have you tried using the data contained in the `IntentType`? Do other applications use the same format of assigning the clipboard value as type `"image/*"`? – Mapsy Jan 30 '15 at 18:22
  • 1
    I actually found out that you don't have to use the clipboard at all. You can just pipe the media you want into the top running Application on the stack. Pretty much mimicking the share command you'd find in the image gallery. **sendIntent.setType("image/*")** works just fine. Supposed to be used with batch uploads, but saves me the trouble of pulling out the file extensions. – HarshMarshmallow Jan 30 '15 at 18:52

0 Answers0