android - 在图库中将图像从一个文件夹复制到另一个文件夹

标签 android copy

在我的应用程序中,我想从图库中选择图像并在 ImageView 中显示图像,还想将图像移动到新文件夹。我在 ImageView 中得到了图像,但它没有移动到另一个文件夹。谁能帮我? 我使用下面的代码来实现这个....

 button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, 1); 

        }
    });

在onActivityResult中

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        System.out.println("File Path Column "+filePathColumn);

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        System.out.println("Column Index "+columnIndex);
        picturePath = cursor.getString(columnIndex);
        cursor.close();
        //imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize = 3;
        Bitmap preview_bitmap=BitmapFactory.decodeFile(picturePath,options);
        System.out.println("Image patha in Messaging "+picturePath);
        imageView.setImageBitmap(preview_bitmap);
        differentpic.setText("Click on Upload if you wish to solve a different pic");
        differentpic.setPadding(0, 0, 0, 30);

        OutputStream out;
        File direct = new File(Environment.getExternalStorageDirectory()
                + "/Solve");

        if (!direct.exists()) {
            direct.mkdirs();
        }
        File file = new File(Environment.getExternalStorageDirectory()+ "/Solve"+String.valueOf(System.currentTimeMillis())+"jpg");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
        file.createNewFile();
        out = new FileOutputStream(file);                       
        //Bitmap bmp = intent.getExtras().get("data");
        //ByteArrayOutputStream stream = new ByteArrayOutputStream();

        int bytes = preview_bitmap.getByteCount();
      //or we can calculate bytes this way. Use a different value than 4 if you don't use 32bit images.
      //int bytes = b.getWidth()*b.getHeight()*4; 

      ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
      preview_bitmap.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

      byte[] array = buffer.array();


            out.write(array);
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        // TODO Auto-generated catch block


    }

 }

我做错了什么?

提前致谢

最佳答案

如果您同时拥有文件 sourceFile 和 destinationFile 的路径,则可以使用以下代码复制文件。

/**
 * copies content from source file to destination file
 * 
 * @param sourceFile
 * @param destFile
 * @throws IOException
 */
private void copyFile(File sourceFile, File destFile) throws IOException {
    if (!sourceFile.exists()) {
        return;
    }

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

}

关于android - 在图库中将图像从一个文件夹复制到另一个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21496894/

相关文章:

java - 复制 AST 中的节点

java - 在java中复制路径包含空格的文件

java - 如何在新线程中创建数组的副本

Android AppCompat v21 提供SwitchCompat 不提供SwitchCompatPreference

android - proguardFile 问题,返回 null : 1

android - 您的免安装应用 APK 的版本代码为 'X',该版本已在使用中

javascript - _.clone 在 lodash 中不起作用?

java - 如何在Android应用程序中保存不确定量的信息?

android - 基地禁止。需要用户

unix - 如何将文件从网络文件夹复制到本地驱动器并保留权限?