How to save file from one folder to another?

Asked

Viewed 479 times

0

I have a listview which displays the files from an external folder (usb). I want to select one of these files and save in an internal folder (Basket).

Here it is when I select an item of this mine listview, i get the position of the item, but from here, do not know how to save this item in the internal folder. I appreciate any help from now on.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int posicao, long l) {
                valor = (String) listView.getItemAtPosition(posicao);
                File arquivo = (File) listView.getItemAtPosition(posicao);

1 answer

0


From what I understand, you can already retrieve the source file. The next step is to only make a copy of the source file.

Use the function below by passing the source and destination files as parameters;

public static void copy(File src, File dst) throws IOException {
try (InputStream in = new FileInputStream(src)) {
    try (OutputStream out = new FileOutputStream(dst)) {
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    }
}
}

You can see more here.

  • Thank you very much. It’s helping a lot, but since I’m a beginner I have some difficulties, in case the origin I can pass by parameter, but the destination I created so File Folder = new File(Environment.getExternalStorageDirectory() + File.separator + "Basket"); hence when I pass this by parameter, it is of the type java.lang.String, and I am going through parameters of the type java.io.File. I would have to convert this to io.File, but I don’t know how yet.

  • If your file folder is being created correctly this shouldn’t happen. But it seems to me that there is an error in your code, method getExternalStorageDirectory() returns an object of type File according to the very reference of android. If you want to search the address use: getExternalStorageDirectory().getPath() this yes will return the address. Remember to be careful as they are not all devices that have SD card.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.