I am looking for an effective and efficient approach to implement Copy paste functionality. How is this achievable using ClipBoardManager Class. Everywhere it is shown how to copy text suing clip data. I want to copy a file or maybe a folder. Thanks in advance
Asked
Active
Viewed 1,201 times
0
-
Possible duplicated question? http://stackoverflow.com/questions/238284/how-to-copy-text-programmatically-in-my-android-app – Felipe Mosso Apr 08 '15 at 12:20
2 Answers
-1
ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
Copying data
ClipData myClip;
String text = "hello world";
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
Pasting data
ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();

ProgramFOX
- 6,131
- 11
- 45
- 51

Ashish Rathee
- 449
- 3
- 16
-
can't i copy something other than text, my requirement is to copy a file or a directory. :'( – puneet dubey mudia Apr 08 '15 at 12:44
-
-
I have a list of files in a file browser. on long pressing any item there is a copy option.what i want to achieve is ..to copy that file on selecting copy option and to paste it wherever user wants. The list is showing string name of the file objects contained in a arrayList. Thanks aSHISh!! – puneet dubey mudia Apr 09 '15 at 04:56
-2
You may want to take a look at this Android guide:
http://developer.android.com/guide/topics/text/copy-paste.html
As you want to copy/past a file, you should use Java Standard I/O
Here is a method to copy a file from one location to another:
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());
}
}
Referenced link: How to programmatically move, copy and delete files and directories on SD?

Community
- 1
- 1

Felipe Mosso
- 3,907
- 11
- 38
- 61
-
:thanks!! But already i referred mentioned link, but i didn't got much. I am able to understand way to copy text but m unable to copy a file from some location on sd card to another. – puneet dubey mudia Apr 08 '15 at 12:27
-
I'm not sure the right way to move a file from one place to another is to use clipboard.. If you want to move files, you can refer to this link: http://stackoverflow.com/questions/4178168/how-to-programmatically-move-copy-and-delete-files-and-directories-on-sd – Felipe Mosso Apr 08 '15 at 12:34
-
:thanks!! But already i referred mentioned link, but i didn't got much. I am able to understand way to copy text but m unable to copy a file from some location on sd card to another. On this link too it is shown how a text is copied. I need to copy a File or folder. – puneet dubey mudia Apr 08 '15 at 12:37
-
Are you sure you looked to the correct link? the second answer of this question (http://stackoverflow.com/questions/4178168/how-to-programmatically-move-copy-and-delete-files-and-directories-on-sd) have some code sample – Felipe Mosso Apr 08 '15 at 12:41
-
I updated my answer with a new code sample.. Please take a look if this meets your expectation – Felipe Mosso Apr 08 '15 at 13:04