【问题标题】:Copy image from one folder to another - Android将图像从一个文件夹复制到另一个文件夹 - Android
【发布时间】:2018-07-30 08:22:34
【问题描述】:

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

   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;

   }}

【问题讨论】:

    标签: android android-file onactivityresult filechannel


    【解决方案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());
        }
    }
    

    【讨论】:

    • 您传递的路径是否正确?字符串路径 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + [yourDir/yourFile.yourExtension]; ?
    【解决方案2】:

    如果你有源路径和目标路径,那么试试这个

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

    一切顺利

    【讨论】:

    • 如果您使用的是路径而不是文件,请确保先将路径转换为文件类型,然后执行上述代码。它会正常工作)))
    猜你喜欢
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 2020-09-29
    • 2011-08-22
    相关资源
    最近更新 更多