Java Clipboard Image getFilenameFromClipboard(Clipboard clip)

Here you can find the source of getFilenameFromClipboard(Clipboard clip)

Description

Returns filename if clipboard contains it and the file exists, otherwise returns null

License

Open Source License

Parameter

Parameter Description
clip a parameter

Declaration

public static String getFilenameFromClipboard(Clipboard clip) 

Method Source Code

//package com.java2s;
/*/*w  w  w  . j av  a  2s. c  o m*/
 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). 
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 */

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;

import java.io.File;

import java.util.List;

public class Main {
    /**
     * Returns filename if clipboard contains it and the file exists, otherwise
     * returns null
     * 
     * @param clip
     */
    public static String getFilenameFromClipboard(Clipboard clip) {

        if (clip.isDataFlavorAvailable(DataFlavor.javaFileListFlavor)) {
            // check that file exists and is it image
            try {
                List files = (List) clip
                        .getData(DataFlavor.javaFileListFlavor);
                if (files.size() == 1) {
                    File file = (File) files.get(0);
                    if (file.exists())
                        return file.getAbsolutePath();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (clip.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
            try {
                String path = (String) clip
                        .getData(DataFlavor.stringFlavor);
                if (path.length() < 512) {
                    File file = new File(path);
                    if (file.exists()) {
                        return file.getAbsolutePath();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

Related

  1. canPasteImageFromClipboard()
  2. getClipboardImage()
  3. getClipboardImage()
  4. getClipboardImage()
  5. isImageDataFlavor(Clipboard clip)
  6. setClipboardImage(final Image image)
  7. setClipboardImage(final Image image)