将图像从一个文件夹复制到另一个-Android

时间:2018-07-30 08:22:34

标签: android android-file onactivityresult filechannel

大家好,我正在尝试将图像从一个文件夹复制到用户从图库中选择的另一个文件夹。它也不会引发任何错误。请检查以下代码。

   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;

   }}

2 个答案:

答案 0 :(得分:1)

这对我有用,请尝试一下;):

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());
    }
}

答案 1 :(得分:1)

如果您有源路径和目标路径,请尝试使用此路径

    /**
     * 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();
    }

    }

祝一切顺利