我正在寻找一种有效且高效的方法来实现复制粘贴功能。如何使用ClipBoardManager Class实现这一点。它向所有地方展示了如何复制文本起诉剪辑数据。我想要复制文件或文件夹。提前致谢
答案 0 :(得分:-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();
答案 1 :(得分:-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?