繁体   English   中英

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

[英]Android copy file from internal storage to external

我试图将文件从内部存储卡复制到外部存储卡通过谷歌搜索我找到了这个答案

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只读文件系统

尝试这样的事情:

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

试试这个,替换这一行:

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

查看此链接,可能会有帮助: 点击此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM