Android.Content.ClipData Class
Representation of a clipped data on the clipboard.

See Also: ClipData Members

Syntax

[Android.Runtime.Register("android/content/ClipData", DoNotGenerateAcw=true)]
public class ClipData : Java.Lang.Object, Android.OS.IParcelable, IDisposable

Remarks

Representation of a clipped data on the clipboard.

ClippedData is a complex type containing one or Item instances, each of which can hold one or more representations of an item of data. For display to the user, it also has a label and iconic representation.

A ClipData contains a Android.Content.ClipDescription, which describes important meta-data about the clip. In particular, its ClipDescription.GetMimeType(int) must return correct MIME type(s) describing the data in the clip. For help in correctly constructing a clip with the correct MIME type, use ClipData.NewPlainText(Java.Lang.ICharSequence, Java.Lang.ICharSequence), ClipData.NewUri(ContentResolver, Java.Lang.ICharSequence, Java.Lang.ICharSequence), and ClipData.NewIntent(Java.Lang.ICharSequence, Android.Content.Intent).

Each Item instance can be one of three main classes of data: a simple CharSequence of text, a single Intent object, or a Uri. See NoType:android/content/ClipData$Item;Href=../../../reference/android/content/ClipData.Item.html for more details.

Developer Guides

For more information about using the clipboard framework, read the Copy and Paste developer guide.

Implementing Paste or Drop

To implement a paste or drop of a ClippedData object into an application, the application must correctly interpret the data for its use. If the NoType:android/content/ClipData$Item;Href=../../../reference/android/content/ClipData.Item.html it contains is simple text or an Intent, there is little to be done: text can only be interpreted as text, and an Intent will typically be used for creating shortcuts (such as placing icons on the home screen) or other actions.

If all you want is the textual representation of the clipped data, you can use the convenience method NoType:android/content/ClipData$Item;Href=../../../reference/android/content/ClipData.Item.html#coerceToText(android.content.Context). In this case there is generally no need to worry about the MIME types reported by ClipDescription.GetMimeType(int), since any clip item can always be converted to a string.

More complicated exchanges will be done through URIs, in particular "content:" URIs. A content URI allows the recipient of a ClippedData item to interact closely with the ContentProvider holding the data in order to negotiate the transfer of that data. The clip must also be filled in with the available MIME types; ClipData.NewUri(ContentResolver, Java.Lang.ICharSequence, Java.Lang.ICharSequence) will take care of correctly doing this.

For example, here is the paste function of a simple NotePad application. When retrieving the data from the clipboard, it can do either two things: if the clipboard contains a URI reference to an existing note, it copies the entire structure of the note into a new note; otherwise, it simply coerces the clip into text and uses that as the new note's contents.

java Example

/**
 * A helper method that replaces the note's data with the contents of the clipboard.
 */
private final void performPaste() {

    // Gets a handle to the Clipboard Manager
    ClipboardManager clipboard = (ClipboardManager)
            getSystemService(Context.CLIPBOARD_SERVICE);

    // Gets a content resolver instance
    ContentResolver cr = getContentResolver();

    // Gets the clipboard data from the clipboard
    ClipData clip = clipboard.getPrimaryClip();
    if (clip != null) {

        String text=null;
        String title=null;

        // Gets the first item from the clipboard data
        ClipData.Item item = clip.getItemAt(0);

        // Tries to get the item's contents as a URI pointing to a note
        Uri uri = item.getUri();

        // Tests to see that the item actually is an URI, and that the URI
        // is a content URI pointing to a provider whose MIME type is the same
        // as the MIME type supported by the Note pad provider.
        if (uri != null && NotePad.Notes.CONTENT_ITEM_TYPE.equals(cr.getType(uri))) {

            // The clipboard holds a reference to data with a note MIME type. This copies it.
            Cursor orig = cr.query(
                    uri,            // URI for the content provider
                    PROJECTION,     // Get the columns referred to in the projection
                    null,           // No selection variables
                    null,           // No selection variables, so no criteria are needed
                    null            // Use the default sort order
            );

            // If the Cursor is not null, and it contains at least one record
            // (moveToFirst() returns true), then this gets the note data from it.
            if (orig != null) {
                if (orig.moveToFirst()) {
                    int colNoteIndex = mCursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_NOTE);
                    int colTitleIndex = mCursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_TITLE);
                    text = orig.getString(colNoteIndex);
                    title = orig.getString(colTitleIndex);
                }

                // Closes the cursor.
                orig.close();
            }
        }

        // If the contents of the clipboard wasn't a reference to a note, then
        // this converts whatever it is to text.
        if (text == null) {
            text = item.coerceToText(this).toString();
        }

        // Updates the current note with the retrieved title and text.
        updateNote(text, title);
    }
}

In many cases an application can paste various types of streams of data. For example, an e-mail application may want to allow the user to paste an image or other binary data as an attachment. This is accomplished through the ContentResolver ContentResolver.GetStreamTypes(Android.Net.Uri, System.String) and ContentResolver.OpenTypedAssetFileDescriptor(Android.Net.Uri, System.String, System.String) methods. These allow a client to discover the type(s) of data that a particular content URI can make available as a stream and retrieve the stream of data.

For example, the implementation of NoType:android/content/ClipData$Item;Href=../../../reference/android/content/ClipData.Item.html#coerceToText(android.content.Context) itself uses this to try to retrieve a URI clip as a stream of text:

java Example

public CharSequence coerceToText(Context context) {
    // If this Item has an explicit textual value, simply return that.
    CharSequence text = getText();
    if (text != null) {
        return text;
    }

    // If this Item has a URI value, try using that.
    Uri uri = getUri();
    if (uri != null) {

        // First see if the URI can be opened as a plain text stream
        // (of any sub-type).  If so, this is the best textual
        // representation for it.
        FileInputStream stream = null;
        try {
            // Ask for a stream of the desired type.
            AssetFileDescriptor descr = context.getContentResolver()
                    .openTypedAssetFileDescriptor(uri, "text/*", null);
            stream = descr.createInputStream();
            InputStreamReader reader = new InputStreamReader(stream, "UTF-8");

            // Got it...  copy the stream into a local string and return it.
            StringBuilder builder = new StringBuilder(128);
            char[] buffer = new char[8192];
            int len;
            while ((len=reader.read(buffer)) > 0) {
                builder.append(buffer, 0, len);
            }
            return builder.toString();

        } catch (FileNotFoundException e) {
            // Unable to open content URI as text...  not really an
            // error, just something to ignore.

        } catch (IOException e) {
            // Something bad has happened.
            Log.w("ClippedData", "Failure loading text", e);
            return e.toString();

        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                }
            }
        }

        // If we couldn't open the URI as a stream, then the URI itself
        // probably serves fairly well as a textual representation.
        return uri.toString();
    }

    // Finally, if all we have is an Intent, then we can just turn that
    // into text.  Not the most user-friendly thing, but it's something.
    Intent intent = getIntent();
    if (intent != null) {
        return intent.toUri(Intent.URI_INTENT_SCHEME);
    }

    // Shouldn't get here, but just in case...
    return "";
}

Implementing Copy or Drag

To be the source of a clip, the application must construct a ClippedData object that any recipient can interpret best for their context. If the clip is to contain a simple text, Intent, or URI, this is easy: an NoType:android/content/ClipData$Item;Href=../../../reference/android/content/ClipData.Item.html containing the appropriate data type can be constructed and used.

More complicated data types require the implementation of support in a ContentProvider for describing and generating the data for the recipient. A common scenario is one where an application places on the clipboard the content: URI of an object that the user has copied, with the data at that URI consisting of a complicated structure that only other applications with direct knowledge of the structure can use.

For applications that do not have intrinsic knowledge of the data structure, the content provider holding it can make the data available as an arbitrary number of types of data streams. This is done by implementing the ContentProvider ContentProvider.GetStreamTypes(Android.Net.Uri, System.String) and ContentProvider.OpenTypedAssetFile(Android.Net.Uri, System.String, System.String) methods.

Going back to our simple NotePad application, this is the implementation it may have to convert a single note URI (consisting of a title and the note text) into a stream of plain text data.

java Example

/**
 * This describes the MIME types that are supported for opening a note
 * URI as a stream.
 */
static ClipDescription NOTE_STREAM_TYPES = new ClipDescription(null,
        new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN });

/**
 * Returns the types of available data streams.  URIs to specific notes are supported.
 * The application can convert such a note to a plain text stream.
 *
 * @param uri the URI to analyze
 * @param mimeTypeFilter The MIME type to check for. This method only returns a data stream
 * type for MIME types that match the filter. Currently, only text/plain MIME types match.
 * @return a data stream MIME type. Currently, only text/plan is returned.
 * @throws IllegalArgumentException if the URI pattern doesn't match any supported patterns.
 */
@Override
public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
    /**
     *  Chooses the data stream type based on the incoming URI pattern.
     */
    switch (sUriMatcher.match(uri)) {

        // If the pattern is for notes or live folders, return null. Data streams are not
        // supported for this type of URI.
        case NOTES:
        case LIVE_FOLDER_NOTES:
            return null;

        // If the pattern is for note IDs and the MIME filter is text/plain, then return
        // text/plain
        case NOTE_ID:
            return NOTE_STREAM_TYPES.filterMimeTypes(mimeTypeFilter);

            // If the URI pattern doesn't match any permitted patterns, throws an exception.
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
}


/**
 * Returns a stream of data for each supported stream type. This method does a query on the
 * incoming URI, then uses
 * {@link android.content.ContentProvider#openPipeHelper(Uri, String, Bundle, Object,
 * PipeDataWriter)} to start another thread in which to convert the data into a stream.
 *
 * @param uri The URI pattern that points to the data stream
 * @param mimeTypeFilter A String containing a MIME type. This method tries to get a stream of
 * data with this MIME type.
 * @param opts Additional options supplied by the caller.  Can be interpreted as
 * desired by the content provider.
 * @return AssetFileDescriptor A handle to the file.
 * @throws FileNotFoundException if there is no file associated with the incoming URI.
 */
@Override
public AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeTypeFilter, Bundle opts)
        throws FileNotFoundException {

    // Checks to see if the MIME type filter matches a supported MIME type.
    String[] mimeTypes = getStreamTypes(uri, mimeTypeFilter);

    // If the MIME type is supported
    if (mimeTypes != null) {

        // Retrieves the note for this URI. Uses the query method defined for this provider,
        // rather than using the database query method.
        Cursor c = query(
                uri,                    // The URI of a note
                READ_NOTE_PROJECTION,   // Gets a projection containing the note's ID, title,
                                        // and contents
                null,                   // No WHERE clause, get all matching records
                null,                   // Since there is no WHERE clause, no selection criteria
                null                    // Use the default sort order (modification date,
                                        // descending
        );


        // If the query fails or the cursor is empty, stop
        if (c == null || !c.moveToFirst()) {

            // If the cursor is empty, simply close the cursor and return
            if (c != null) {
                c.close();
            }

            // If the cursor is null, throw an exception
            throw new FileNotFoundException("Unable to query " + uri);
        }

        // Start a new thread that pipes the stream data back to the caller.
        return new AssetFileDescriptor(
                openPipeHelper(uri, mimeTypes[0], opts, c, this), 0,
                AssetFileDescriptor.UNKNOWN_LENGTH);
    }

    // If the MIME type is not supported, return a read-only handle to the file.
    return super.openTypedAssetFile(uri, mimeTypeFilter, opts);
}

/**
 * Implementation of {@link android.content.ContentProvider.PipeDataWriter}
 * to perform the actual work of converting the data in one of cursors to a
 * stream of data for the client to read.
 */
@Override
public void writeDataToPipe(ParcelFileDescriptor output, Uri uri, String mimeType,
        Bundle opts, Cursor c) {
    // We currently only support conversion-to-text from a single note entry,
    // so no need for cursor data type checking here.
    FileOutputStream fout = new FileOutputStream(output.getFileDescriptor());
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(new OutputStreamWriter(fout, "UTF-8"));
        pw.println(c.getString(READ_NOTE_TITLE_INDEX));
        pw.println("");
        pw.println(c.getString(READ_NOTE_NOTE_INDEX));
    } catch (UnsupportedEncodingException e) {
        Log.w(TAG, "Ooops", e);
    } finally {
        c.close();
        if (pw != null) {
            pw.flush();
        }
        try {
            fout.close();
        } catch (IOException e) {
        }
    }
}

The copy operation in our NotePad application is now just a simple matter of making a clip containing the URI of the note being copied:

java Example

case R.id.context_copy:
    // Gets a handle to the clipboard service.
    ClipboardManager clipboard = (ClipboardManager)
            getSystemService(Context.CLIPBOARD_SERVICE);

    // Copies the notes URI to the clipboard. In effect, this copies the note itself
    clipboard.setPrimaryClip(ClipData.newUri(   // new clipboard item holding a URI
            getContentResolver(),               // resolver to retrieve URI info
            "Note",                             // label for the clip
            noteUri)                            // the URI
    );

    // Returns to the caller and skips further processing.
    return true;

Note if a paste operation needs this clip as text (for example to paste into an editor), then NoType:android/content/ClipData$Item;Href=../../../reference/android/content/ClipData.Item.html#coerceToText(android.content.Context) will ask the content provider for the clip URI as text and successfully paste the entire note.

[Android Documentation]

Requirements

Namespace: Android.Content
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 11