0

Hi Everyone I am trying to copy an image from one folder to another which user selects from the gallery. It's not throwing any error as well. Please check the below code.

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    String fileName = "";

    if (resultCode == RESULT_OK) {
    if (requestCode == GALLERY) {
            try {
                Uri selectedImageUri = data.getData();

                String path = getPathFromURI(selectedImageUri);
                switch (cameraNo) {
                    case 1:
                        Bitmap bitmap1 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
                        imageBtn1.setImageBitmap(bitmap1);
                        reduceImageSize(path);
                        fileName = path.substring(path.lastIndexOf("/")+1);
              try {
                            File sd = Environment.getExternalStorageDirectory();

                            if (sd.canWrite()) {

                                String destinationImagePath= "/MyImages/file.jpg";
                                File source= new File(path);
                                File destination= new File(sd, destinationImagePath);
                                if (source.exists()) {
                                    FileChannel src = new FileInputStream(source).getChannel();
                                    FileChannel dst = new FileOutputStream(destination).getChannel();
                                    dst.transferFrom(src, 0, src.size());
                                    src.close();
                                    dst.close();
                                }
                            }
                        } catch (Exception e) {
                        }

                        imageArrayList.add(path);
                        imageNameList.add(fileName);

                        break;

   }}
Mac_Play
  • 302
  • 4
  • 21

2 Answers2

3

this is working for me, give it a try ;):

public static void copyFile(String inputPath, String outputPath) {

    InputStream in = null;
    OutputStream out = null;
    try {
        in = new FileInputStream(inputPath);
        out = new FileOutputStream(outputPath);

        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;

        LOGGER.debug("Copied file to " + outputPath);

    } catch (FileNotFoundException fnfe1) {
        LOGGER.error(fnfe1.getMessage());
    } catch (Exception e) {
        LOGGER.error("tag", e.getMessage());
    }
}
Tomás Rodrigues
  • 519
  • 2
  • 17
  • are you passing the correct paths? String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + [yourDir/yourFile.yourExtension]; ? – Tomás Rodrigues Jul 30 '18 at 08:51
3

if you have a source path and destination path then try this one

    /**
     * copy contents from source file to destination file
     * 
     * @param sourceFilePath  Source file path address
     * @param destinationFilePath Destination file path address
     */
    private void copyFile(File sourceFilePath, File destinationFilePath) {

        try{

            if (!sourceFilePath.exists()) {
            return;
            }

        FileChannel source = null;
        FileChannel destination = null;
        source = new FileInputStream(sourceFilePath).getChannel();
        destination = new FileOutputStream(destinationFilePath).getChannel();
        if (destination != null && source != null) {
            destination.transferFrom(source, 0, source.size());
        }
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }

    }catch(Exception ex){
    ex.printStackTrace();
    }

    }

All the best

Sagar Bhagwat
  • 103
  • 1
  • 9
  • If you are using the path instead of File then make sure to convert your path into the File type first and then execute the above code. It will work fine))) – Haris Abdullah Mar 09 '22 at 05:36