copy file To Clipboard - Android android.content

Android examples for android.content:ClipboardManager

Description

copy file To Clipboard

Demo Code

import android.annotation.SuppressLint;
import android.content.ClipDescription;
import android.content.Context;
import android.net.Uri;
import android.os.StatFs;
import android.util.Log;
import android.webkit.URLUtil;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class Main{

    /**/*from  w w w.j  a  v  a2s .com*/
     * copy file to the clipboard
     * @param context - Context
     * @param file - the file to copy
     */
    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public static void copyToClipboard(Context context, File file) {

        Log.d(TAG, "copyToClipboard " + file.getAbsolutePath());

        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setText(FILE_PREFIX + file.getAbsolutePath());

        } else {
            Log.d(TAG, "copy" + file.getAbsolutePath());
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            Uri uri = Uri.fromFile(file);

            Log.d(TAG, "getFileFromClipboard() uri= " + uri);

            android.content.ClipData clip = android.content.ClipData
                    .newRawUri(null, uri);
            clipboard.setPrimaryClip(clip);

        }

    }

}

Related Tutorials