Copy Files/Folder to Clipboard, to allow Manual Pasting to Android through Windows Explorer (via MTP)

坚强是说给别人听的谎言 提交于 2019-12-11 15:33:17

问题


Ideally in Java, but perhaps in C#, I'm looking to programmatically copy a directory of files & folders into the clipboard, and allow the user to manually paste these through Windows Explorer (Ctr+V etc) onto their Android device, via Windows Explorer.

So the code below is from the following question and is what I've tried so far:- https://stackoverflow.com/a/31798747/6120066

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CopyFile {

    public static void main(String[] args) throws InterruptedException {
        File file = new File("C:\\blabla.txt");
        List listOfFiles = new ArrayList();
        listOfFiles.add(file);

        FileTransferable ft = new FileTransferable(listOfFiles);

        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ft, new ClipboardOwner() {
            @Override
            public void lostOwnership(Clipboard clipboard, Transferable contents) {
                System.out.println("Lost ownership");
            }
        });
        System.out.println("WAITING");
        Thread.sleep(2 * 60 * 1000);
    }

    public static class FileTransferable implements Transferable {

        private List listOfFiles;

        public FileTransferable(List listOfFiles) {
            this.listOfFiles = listOfFiles;
        }

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[]{DataFlavor.javaFileListFlavor};
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return DataFlavor.javaFileListFlavor.equals(flavor);
        }

        @Override
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            return listOfFiles;
        }
    }
}

I've managed to copy files and paste them manually to another location on my laptop's disk, but it doesn't work when I try pasting to the Android device. However if I manually copy a file/folder in Explorer, I can paste it onto the Android device fine.

Any ideas? Do I need to use more native stuff? That is why I tagged with C# also.

来源:https://stackoverflow.com/questions/58302056/copy-files-folder-to-clipboard-to-allow-manual-pasting-to-android-through-windo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!