首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android中以编程方式将图像文件从Gallery复制到另一个文件夹

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

Stack Overflow用户
提问于 2011-12-29 13:49:00
回答 4查看 48.1K关注 0票数 21

我想从画廊中挑选图像,并将其复制到SDCard的其他文件夹中。

从图库中选取图像的代码

代码语言:javascript
复制
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);

我将此URI onActivityResult获取到content://media/external/images/media/681

我想复制图像,

表单path ="content://media/external/images/media/681

在Android中path = "file:///mnt/sdcard/sharedresources/ sdcard的这条路径。

该怎么做呢?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-12-29 14:16:30

代码语言:javascript
复制
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();

希望能对你有所帮助

票数 13
EN

Stack Overflow用户

发布于 2011-12-30 14:39:05

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

代码语言:javascript
复制
     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);
    }
票数 36
EN

Stack Overflow用户

发布于 2015-07-13 16:41:04

在阅读this link时,他们在这里讨论了四种在Java中复制文件的方法,因此也适用于android。

虽然作者的结论是使用@Prashant答案中使用的'channel‘是最好的方法,但您甚至可以探索其他方法。

(我已经尝试了前两个,它们都能找到)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8664440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档