【问题标题】:Android copy file from internal storage to externalAndroid将文件从内部存储复制到外部
【发布时间】:2015-04-22 07:26:29
【问题描述】:

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

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

【问题讨论】:

  • 发布 logcat 输出以获得更好的想法!
  • @PareshMayani /storage/sdcard:open 失败:EROFS(只读文件系统)
  • 在您的问题中包含完整的 logcat 输出!
  • 您正在将此位置用于内部存储器:“/storage/sdcard/”。您确定这是用于内部存储吗?
  • @RahulSharma 是的,内部副本效果很好

标签: java android


【解决方案1】:

试试这样的:

 new FileAsyncTask().execute(files);

// 后台进程的异步任务

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

// 复制文件到SDCard的函数

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

【讨论】:

    【解决方案2】:

    试试这个,替换这一行:

    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

    【讨论】:

      猜你喜欢
      • 2015-09-14
      • 2023-03-05
      • 2011-12-26
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      相关资源
      最近更新 更多