如何在Android中以编程方式将图像文件从图库复制到另一个文件夹

Pra*_*dam 19 android android-intent android-gallery android-contentprovider android-file

我想从Gallery中选择图像并将其复制到SDCard中的其他文件夹中.

从图库中选择图像的代码

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);
Run Code Online (Sandbox Code Playgroud)

我在content://media/external/images/media/681onActivityResult上获得了这个URI.

我想复制图片,

形成 path ="content://media/external/images/media/681

path = "file:///mnt/sdcard/sharedresources/ SD卡中的Android这条道路.

这该怎么做?

Pra*_*dam 34

感谢所有...工作代码在这里..

     private OnClickListener photoAlbumListener = new OnClickListener(){
          @Override
          public void onClick(View arg0) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
            imagepath = Environment.getExternalStorageDirectory()+"/sharedresources/"+HelperFunctions.getDateTimeForFileName()+".png";
            uriImagePath = Uri.fromFile(new File(imagepath));
            photoPickerIntent.setType("image/*");
            photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriImagePath);
            photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());
            photoPickerIntent.putExtra("return-data", true);
            startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);

          }
      };






   protected void onActivityResult(int requestCode, int resultCode, Intent data) {


           if (resultCode == RESULT_OK) {
                switch(requestCode){


                 case 22:
                        Log.d("onActivityResult","uriImagePath Gallary :"+data.getData().toString());
                        Intent intentGallary = new Intent(mContext, ShareInfoActivity.class);
                        intentGallary.putExtra(IMAGE_DATA, uriImagePath);
                        intentGallary.putExtra(TYPE, "photo");
                        File f = new File(imagepath);
                        if (!f.exists())
                        {
                            try {
                                f.createNewFile();
                                copyFile(new File(getRealPathFromURI(data.getData())), f);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                        startActivity(intentGallary);
                        finish();
                 break;


                }
              }





   }

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


    }

    private String getRealPathFromURI(Uri contentUri) {

       String[] proj = { MediaStore.Video.Media.DATA };
       Cursor cursor = managedQuery(contentUri, proj, null, null, null);
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       cursor.moveToFirst();
       return cursor.getString(column_index);
    }
Run Code Online (Sandbox Code Playgroud)


Ric*_*cha 13

OutputStream out;
            String root = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
            File createDir = new File(root+"Folder Name"+File.separator);
            if(!createDir.exists()) {
                createDir.mkdir();
            }
            File file = new File(root + "Folder Name" + File.separator +"Name of File");
            file.createNewFile();
            out = new FileOutputStream(file);                       

        out.write(data);
        out.close();
Run Code Online (Sandbox Code Playgroud)

希望它会对你有所帮助

  • out.write(数据); 什么是"数据"? (21认同)

AAn*_*kit 5

一种解决方案是

1)从选择的文件的inputStream中读取字节。

我在此URI onActivityResult上获得“ content:// media / external / images / media / 681”。您可以通过查询此Uri来获取文件名。得到它的inputStream。将其读取为byte []。

干得好/

Uri u = Uri.Parse(“ content:// media / external / images / media / 681”);

游标cursor = contentResolver.query(u,null,null,null,null); 有一列名称“ _data”将返回您的文件名,您可以从文件名创建输入流,

您现在可以阅读此输入流

         byte data=new byte[fis.available()];
          fis.read(data);
Run Code Online (Sandbox Code Playgroud)

所以你有图像字节的数据(字节数组)

2)在sdcard上创建一个文件,并使用第一步中的byte []进行写入。

       File file=new File(fileOnSD.getAbsolutePath() +"your foldername", fileName);
        FileOutputStream fout=new FileOutputStream(file, false);
        fout.write(data);
Run Code Online (Sandbox Code Playgroud)

作为查询方法中已有的fileName,请在此处使用相同的名称。