我正在寻找一种有效且高效的方法来实现复制粘贴功能。使用 ClipBoardManager 类如何实现这一点。到处都显示了如何复制使用剪辑数据的文本。我想复制一个文件或者一个文件夹。提前致谢
问问题
1147 次
2 回答
-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();
于 2015-04-08T12:29:46.010 回答
-2
你可能想看看这个 Android 指南:
http://developer.android.com/guide/topics/text/copy-paste.html
当你想复制/粘贴一个文件时,你应该使用Java 标准 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());
}
}
于 2015-04-08T12:15:48.810 回答