ARTICLE AD BOX
The Android application I am writing uses the clipboard to allow to copy/paste information between misc. locations in the app, but for some reasons the clipboard does not return the data using the intended MIME-type, even though the application has previously copied the data to the clipboard specifying that very same MIME-type:
This is the code that copies the data to the clipboard:
... String data = convertToJSON(object); // the data that I want to copy-paste (an object representad as text using JSON syntax) String mimeType = "application/vnd.myapp.items+json"; ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setPrimaryClip(new ClipData("items", new String[] { mimeType }, new ClipData.Item(data))); ...And this is the code that should retrieve that very same data again from the clipboard:
... ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = clipboard.getPrimaryClip(); if (clip != null && clip.getItemCount() > 0) { ClipData.Item clipItem = clip.getItemAt(0); ClipDescription clipDesc = clip.getDescription(); String[] availableMimeTypes = clipDesc.filterMimeTypes("*/*"); if (availableMimeTypes != null) { Collection<String> availableMimeTypesList = Arrays.asList(availableMimeTypes); Log.v(TAG, "got " + clipDesc.getMimeTypeCount() + " MIME-types: " + availableMimeTypesList); if (availableMimeTypesList.contains("application/vnd.myapp.items+json")) { // *** the above check always fails and the data is never retrieved :-( *** String clipboardContent = clipItem.coerceToText(this).toString(); Object x = convertFromJSON(clipboardContent); ... } ... } } ...For some (at least for me) mysterious reason the Log displaying all available MIME-types always yields "got 1 MIME-types: [text/plain]" as the only available even though I just copied data as
"application/vnd.myapp.items+json"to the clipboard just seconds ago. Any idea, why that is so? What is wrong with the above code? What am I missing?
I am 100% positive that exactly this code has worked before in earlier versions (Android versions as well as AndroidStudio versions, that is), but for some odd reason it has seized to work and I am baffled, why that is so. Any idea or explanation anyone?
My application's current target API is "33".
