我正在寻找一种有效和高效的方法来实现复制粘贴功能。这是如何使用ClipBoardManager类来实现的。到处都显示了如何使用剪辑数据复制文本。我想复制一个文件或文件夹。提前感谢
发布于 2015-04-08 20:29:46
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();发布于 2015-04-08 20:15:49
你可能想看看这个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?
https://stackoverflow.com/questions/29514280
复制相似问题