首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ClipBoardManager android在文件管理器中复制/粘贴实现

使用ClipBoardManager android在文件管理器中复制/粘贴实现
EN

Stack Overflow用户
提问于 2015-04-08 20:12:36
回答 2查看 1.1K关注 0票数 0

我正在寻找一种有效和高效的方法来实现复制粘贴功能。这是如何使用ClipBoardManager类来实现的。到处都显示了如何使用剪辑数据复制文本。我想复制一个文件或文件夹。提前感谢

EN

回答 2

Stack Overflow用户

发布于 2015-04-08 20:29:46

代码语言:javascript
复制
ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

正在复制数据

代码语言:javascript
复制
ClipData myClip;
String text = "hello world";
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);

粘贴数据

代码语言:javascript
复制
ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();
票数 -1
EN

Stack Overflow用户

发布于 2015-04-08 20:15:49

你可能想看看这个Android指南:

http://developer.android.com/guide/topics/text/copy-paste.html

当您想要复制/传递文件时,您应该使用Java Standard I/O

下面是一种将文件从一个位置复制到另一个位置的方法:

代码语言:javascript
复制
private void copyFile(String inputPath, String inputFile, String outputPath) {

    InputStream in = null;
    OutputStream out = null;
    try {

        //create output directory if it doesn't exist
        File dir = new File (outputPath); 
        if (!dir.exists())
        {
            dir.mkdirs();
        }


        in = new FileInputStream(inputPath + inputFile);        
        out = new FileOutputStream(outputPath + inputFile);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;

            // write the output file (You have now copied the file)
            out.flush();
        out.close();
        out = null;        

    }  catch (FileNotFoundException fnfe1) {
        Log.e("tag", fnfe1.getMessage());
    }
            catch (Exception e) {
        Log.e("tag", e.getMessage());
    }

}

参考链接:How to programmatically move, copy and delete files and directories on SD?

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29514280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档