Get uri from Clipboard - Android android.content

Android examples for android.content:Clipboard

Description

Get uri from Clipboard

Demo Code

import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class Main{

    /**//  w  w  w . j av  a  2s  .  com
     * 
     *
     * @param context
     * @return
     */
    public static Uri getUri(Context context) {
        ClipboardManager clipboard = (ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard.hasPrimaryClip()
                && clipboard.getPrimaryClipDescription().hasMimeType(
                        ClipDescription.MIMETYPE_TEXT_URILIST)) {
            ClipData clip = clipboard.getPrimaryClip();
            if (clip != null && clip.getItemCount() > 0) {
                return clip.getItemAt(0).getUri();
            }
        }
        return null;
    }
}

Related Tutorials