Android 5.1.1无法以编程方式在外部SD卡中创建文件夹?

时间:2015-11-05 06:47:39

标签: android

我正在尝试创建一个应用程序,将媒体文件夹从手机内存复制到我的三星j5上的外部SD卡。我能够从内部存储器复制到内部存储器。但无法复制到外部SD卡。

static String Source_Location=Environment.getExternalStorageDirectory().toString()+"/WhatsApp/Media/WallPaper";
static String Target_Location= System.getenv("SECONDARY_STORAGE");// "/mnt/extSdCard/";

TextView strDisplaypath;
    FileInputStream in;
    FileOutputStream out;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        strDisplaypath= (TextView) findViewById(R.id.lbldiaply);
    File SrcPath=new File(Source_Location);
        File DestPath=new File(Target_Location);
    try { 
            copyDirectory(SrcPath, DestPath);
           } 
    catch (IOException e) { 
            e.printStackTrace();
        }
    }

    public void copyDirectory(File sourceLocation , File targetLocation)
            throws IOException {

        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

            String[] children = sourceLocation.list();
            for (int i=0; i<children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]),
                        new File(targetLocation, children[i]));
            }
        } else {

            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetLocation);

            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
        Toast.makeText(getApplicationContext(),"Files Copied",Toast.LENGTH_LONG).show();
    }

0 个答案:

没有答案