复制文件从目录到另一个,具有路径的文件和目录, [英] Copy file from the directory to another , having the path's of file and directory

查看:210
本文介绍了复制文件从目录到另一个,具有路径的文件和目录,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我想从一个目录拷贝文件到另一个,我有文件的路径文件路径,并有目录 dirPath 在什么我必须复制文件。我尝试过很多办法,但没有任何帮助,某些方面只是做一些空(0 KB)文件奇怪的名字从我的文件的名称不同。因此,帮助请:)

这里是code某部分,如果它帮你呀,我有两个按钮画廊和相机,我必须挑选有图像

 按钮btnCam =(按钮)dialog.findViewById(R.id.btncamera);
                btnCam.setOnClickListener(新View.OnClickListener()
                {
                    @覆盖
                    公共无效的onClick(视图v)
                    {
                        dialog.cancel();
                        意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
                        startActivityForResult(cameraIntent,2500);

                    }
                });
                //拍照键结束


                按钮btnGal =(按钮)dialog.findViewById(R.id.btngalary);
                btnGal.setOnClickListener(新OnClickListener()
                {
                    @覆盖
                    公共无效的onClick(视图v)
                    {
                        dialog.cancel();
                        意图I =新的意图(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(ⅰ,RESULT_LOAD_IMAGE);
                    }
                });
 

和活动成果

  @覆盖
  保护无效onActivityResult(INT申请code,INT结果code,意图数据)
  {
      super.onActivityResult(要求code,因此code,数据);

      如果(要求code == RESULT_LOAD_IMAGE和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!=空数据)
      {
          乌里selectedImage = data.getData();
          URL =新的String(selectedImage.toString());
          //这里我必须复制文件路径`selectedImage`并替换
          //与路径`currentDir.hetPath目录()`
      }
      如果(要求code == 2500)
      {
          。位图照片=(位图)data.getExtras()获得(数据);
          imageView.setImageBitmap(照片);
          //这里我必须做上述同样的事
      }
  }
 

解决方案

我找到了一些办法,在我的活动结果,我必须调用的CopyFile(字符串,字符串)功能,这里是身体

 公共静态布尔的CopyFile(String一个,字符串){
        尝试 {
            文件SD = Environment.getExternalStorageDirectory();
            如果(sd.canWrite()){
                INT端= from.toString()lastIndexOf(/)。
                字符串STR1 = from.toString()子(0,结束)。
                。字符串STR2赛车= from.toString()子(完+ 1,from.length());
                文件源=新的文件(STR1,STR2);
                文件目的地=新的文件(到,STR2);
                如果(source.exists()){
                    FileChannel SRC =新的FileInputStream(源).getChannel();
                    FileChannel DST =新的FileOutputStream(目标).getChannel();
                    dst.transferFrom(源,0,src.size());
                    src.close();
                    dst.close();
                }
            }
            返回true;
        }赶上(例外五){
            返回false;
        }
    }
 

in my android application I want to copy file from one directory to another , I have the path of file filePath , and have the path of directory dirPath in what I must copy the file. I tried many ways , but nothing helped , some ways only make some empty(0 kb) file with strange name different from the name of my file. SO help please :)

here is some part of code, if it help's you, I have two buttons for Gallery and for Camera , and I must pick images from there

Button btnCam = (Button) dialog.findViewById(R.id.btncamera);
                btnCam.setOnClickListener(new View.OnClickListener() 
                {
                    @Override
                    public void onClick(View v) 
                    {
                        dialog.cancel();
                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(cameraIntent, 2500);

                    }
                });
                //end of camera button 


                Button btnGal = (Button) dialog.findViewById(R.id.btngalary);
                btnGal.setOnClickListener(new OnClickListener() 
                {
                    @Override
                    public void onClick(View v) 
                    {
                        dialog.cancel();
                        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(i, RESULT_LOAD_IMAGE);
                    }
                });

and Activity Result

@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();
          url = new String(selectedImage.toString()); 
          //here I must copy file with path `selectedImage` and replace it in 
          // directory with path `currentDir.hetPath()` 
      }
      if (requestCode == 2500) 
      {  
          Bitmap photo = (Bitmap) data.getExtras().get("data"); 
          imageView.setImageBitmap(photo);
          //here I must do the same thing above
      }  
  }

解决方案

I found some way , in my Activity result I must call copyFile(String, String) function , here is the body

public static boolean copyFile(String from, String to) {
        try {
            File sd = Environment.getExternalStorageDirectory();
            if (sd.canWrite()) {
                int end = from.toString().lastIndexOf("/");
                String str1 = from.toString().substring(0, end);
                String str2 = from.toString().substring(end+1, from.length());
                File source = new File(str1, str2);
                File destination= new File(to, str2);
                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();
                }
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    } 

这篇关于复制文件从目录到另一个,具有路径的文件和目录,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆