【问题标题】:Copy/paste implementation in file manager using ClipBoardManager android使用 ClipBoardManager android 在文件管理器中复制/粘贴实现
【发布时间】:2015-04-08 12:12:35
【问题描述】:

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

【问题讨论】:

标签: android copy-paste clipboardmanager


【解决方案1】:
ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

复制数据

ClipData myClip;
String text = "hello world";
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);

粘贴数据

ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();

【讨论】:

  • 我不能复制文本以外的东西,我的要求是复制文件或目录。 :'(
  • 你能解释一下你想要实现什么
  • 我在文件浏览器中有一个文件列表。长按任何项目都有一个复制选项。我想要实现的是..在选择复制选项时复制该文件并将其粘贴到用户想要的任何位置。该列表显示了 arrayList 中包含的文件对象的字符串名称。谢谢阿希什!!
【解决方案2】:

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

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


如果你想复制/粘贴一个文件,你应该使用Java Standard I/O

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

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?

【讨论】:

  • :谢谢!!但是我已经提到了提到的链接,但我没有得到太多。我能够理解复制文本的方式,但无法将文件从 sd 卡上的某个位置复制到另一个位置。
  • 我不确定将文件从一个地方移动到另一个地方的正确方法是使用剪贴板。如果你想移动文件,可以参考这个链接:stackoverflow.com/questions/4178168/…跨度>
  • :谢谢!!但是我已经提到了提到的链接,但我没有得到太多。我能够理解复制文本的方式,但无法将文件从 sd 卡上的某个位置复制到另一个位置。在此链接上也显示了如何复制文本。我需要复制一个文件或文件夹。
  • 您确定您查看的是正确的链接吗?这个问题的第二个答案(stackoverflow.com/questions/4178168/…)有一些代码示例
  • 我用新的代码示例更新了我的答案。请看看这是否符合您的期望
猜你喜欢
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
相关资源
最近更新 更多