12

I would like to copy image from my android application to the other android application using clipboard manager. I have researched a lot and read this tutorial but it doesn't cover the image copying part. The below code which copies image but when I am trying to paste, only the image's path is pasted.

   ContentValues values = new ContentValues(2);
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
    values.put(MediaStore.Images.Media.DATA, "/mnt/sdcard/1.jpg");
    ContentResolver theContent = getContentResolver();
    Uri imageUri = theContent.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    ClipData theClip = ClipData.newUri(getContentResolver(), "Image", imageUri);
    mClipboard.setPrimaryClip(theClip);

I tried to past in apps which support image pasting, so I guess the problem is "copy". Any answers and advises would be appreciated.

user2215256
  • 123
  • 1
  • 1
  • 4

2 Answers2

3

This code works, just find appropriate app and OS to test it.

    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, filename.getAbsolutePath());
    ContentResolver theContent = getContentResolver();
    Uri imageUri = theContent.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    ClipData theClip = ClipData.newUri(getContentResolver(), "Image", imageUri);
    mClipboard.setPrimaryClip(theClip);

Edit: According to my further investigations, however, in most of Android platforms the image copy past is not possible because original android OS does not have such a feature. The code above works only for several cases where the OS is modified. For example, in Samsung Note tablet it works. And you can past the image in Polaris office. But Polaris is used the hidden API provided by Samsung, as that app comes with devices.

ProgDevCode
  • 138
  • 2
  • 13
someUser
  • 965
  • 12
  • 24
  • I tried it with gmail app. It is pasting path of the image not the image.am i doing anything wrong.How to work with it.please let me know. – Akanksha Rathore Dec 23 '13 at 09:19
  • @David_O: I tried this to get the image uri, but when i try to paste it in gmail/text/ or any other app the URI just gets pasted ! How can i get it to paste the image and not the URI ? – user2234 Jun 25 '14 at 09:59
  • OS and apps should support that. There are few OSs and apps which do that. They change to OS and add new API there. Please see my edit. – someUser Jun 25 '14 at 10:27
  • 1
    @AkankshaRathore did u achieved copy and paste of image if yes than please tell me how to achieve it. – Mitesh Jobanputra Apr 16 '16 at 08:55
1

Here is my solution; it works on Samsung Galaxy Note 9:

      Uri uri = FileProvider.getUriForFile(
                this,
                "com.example.android.fileprovider",
                new File(path));
        ClipboardManager mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newUri(getApplicationContext().getContentResolver(), "a Photo", uri);
        mClipboard.setPrimaryClip(clip);
        Toast.makeText(this,"Image copied to clipboard",Toast.LENGTH_SHORT).show();
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83