Android从内部存储复制文件到外部

时间:2015-04-22 07:26:29

标签: java android

我正在尝试将文件从内部存储卡复制到外部存储卡 通过谷歌搜索我发现了这个答案

try {
 InputStream in = new FileInputStream("/storage/sdcard1/bluetooth/file7.zip"); // Memory card path
            File myFile = new File("/storage/sdcard/"); // 
            OutputStream out = new FileOutputStream(myFile);
              // 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();  
              session.showToast("file copied sucessfully");
        } catch (FileNotFoundException e) {
             showToast(e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
             showToast(e.getMessage());
            e.printStackTrace();
        }

其内部移动到内部或外部存储到外部的工作 但交叉传输不起作用,错误Erofs只读文件系统

2 个答案:

答案 0 :(得分:2)

尝试这样的事情:

 new FileAsyncTask().execute(files);

//后台进程的AsyncTask

private class FileAsyncTask extends AsyncTask<ArrayList<String>, Void, Void> {         
    ArrayList<String> files;         
    ProgressDialog dialog;         
    @Override         
    protected void onPreExecute() {             
        dialog = ProgressDialog.show(ActivityName.this, "Your Title", "Loading...");         
    }         
    @Override         
    protected Void doInBackground(ArrayList<String>... params) {              
        files = params[0];             
        for (int i = 0; i < files.size(); i++) {                 
            copyFileToSDCard(files.get(i));                
        }             return null;         
    }         
    @Override         
    protected void onPostExecute(Void result) {             
        dialog.dismiss();         
    }      
} 

//将文件复制到SD卡的功能

public void copyFileToSDCard(String fileFrom){
    AssetManager is = this.getAssets();
    InputStream fis;
    try {

        fis = is.open(fileFrom);
        FileOutputStream fos;
        if (!APP_FILE_PATH.exists()) {
            APP_FILE_PATH.mkdirs();
        }
        fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/MyProject", fileFrom));
        byte[] b = new byte[8];
        int i;
        while ((i = fis.read(b)) != -1) {
            fos.write(b, 0, i);
        }
        fos.flush();
        fos.close();
        fis.close();
    } 
    catch (IOException e1) {
        e1.printStackTrace();
    }
}

public static boolean copyFile(String from, String to) {
try {
    int bytesum = 0;
    int byteread = 0;
    File oldfile = new File(from);
    if (oldfile.exists()) {
        InputStream inStream = new FileInputStream(from);
        FileOutputStream fs = new FileOutputStream(to);
        byte[] buffer = new byte[1444];
        while ((byteread = inStream.read(buffer)) != -1) {
            bytesum += byteread;
            fs.write(buffer, 0, byteread);
        }
        inStream.close();
        fs.close();
    }
    return true;
} catch (Exception e) {
    return false;
}
}

答案 1 :(得分:1)

试试这个,替换这一行:

File myFile = new File("/storage/sdcard/");

使用:

ContextWrapper cw = new ContextWrapper(getApplicationContext());
     // path to /data/data/yourapp/app_data/imageDir
    File myFile = cw.getDir("imageDir", Context.MODE_PRIVATE);

点击此链接,可能会有所帮助:click here