get File From Clipboard - Android android.content

Android examples for android.content:ClipboardManager

Description

get File From 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{

    /**/*www .  jav a 2 s .com*/
     * Gets a File object fron the clipboard  
     * @param context 
     * @param clean - if true then clean the clipboard
     * @return
     */
    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public static File getFileFromClipboard(Context context, boolean clean) {
        Log.d(TAG, "getFileFromClipboard()");

        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);
            if (clipboard.hasText()) {
                String path = clipboard.getText().toString();
                if ((path.length() > 7)
                        && path.substring(0, 7).equals(FILE_PREFIX)) {
                    path = path.substring(6);
                    File file = new File(path);
                    if (file.exists()) {
                        if (clean) {
                            clipboard.setText(null);
                        }
                        return file;
                    }

                }
            }
            return null;
        }

        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);

        Log.d(TAG, "getFileFromClipboard() >= 11 is in clipBoard "
                + clipboard.hasPrimaryClip());
        if (clipboard.hasPrimaryClip()
                && (clipboard.getPrimaryClipDescription()
                        .hasMimeType(ClipDescription.MIMETYPE_TEXT_URILIST))) {
            android.content.ClipData clip = clipboard.getPrimaryClip();
            Log.d(TAG,
                    "getFileFromClipboard() item count: "
                            + clip.getItemCount());

            android.content.ClipData.Item item = clip.getItemAt(0);
            Uri uri = item.getUri();

            Log.d(TAG,
                    "getFileFromClipboard() uri= " + uri + " path:"
                            + uri.getPath() + "  "
                            + URLUtil.isFileUrl(uri.toString()));

            if (URLUtil.isFileUrl(uri.toString())) {
                Log.d(TAG, "getFileFromClipboard() isFileUrl");
                File file = new File(uri.getPath());
                Log.d(TAG,
                        "getFileFromClipboard() uri.getPath()= "
                                + uri.getPath() + "  file: "
                                + file.getAbsolutePath());
                Log.d(TAG,
                        "" + file.getAbsolutePath() + " isFile: "
                                + file.isFile() + "  " + " isDirectory(): "
                                + file.isDirectory()
                                + " file.isAbsolut(): " + file.isAbsolute()
                                + " canWrite" + file.canWrite()
                                + "  canRead: " + file.canRead());

                if (file.exists()) {
                    Log.d(TAG, "getFileFromClipboard() File exists");
                    if (clean) {
                        clip = android.content.ClipData.newPlainText(null,
                                null);
                        clipboard.setPrimaryClip(clip);
                    }
                    return file;
                }
            }
        }

        return null;

    }

}

Related Tutorials