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

时间:2012-11-06 09:03:09

标签: java android android-intent

我试图使用Adobe阅读器阅读从服务器下载的pdf文件问题是当我将其存储在内部存储中时其他应用程序无法读取该文件。现在我想知道如何复制这个文件在外部存储空间(/ sdcard /),以便pdf观众可以查看。

由于安全原因,我将文件存储在内部存储中,然后删除外部存储中的文件。

我的问题是如何复制保存在内部存储中的文件,而不使用raw或资源放入输入流。

    InputStream myInput = getResources().openRawResource(R.raw.p13);

        FileOutputStream fos = openFileOutput("sample", Context.MODE_PRIVATE);

        // transfer bytes from the input file to the output file
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0)
        {
            fos.write(buffer, 0, length);
        }
        // Close the streams
        fos.flush();
        fos.close();
        myInput.close();

2 个答案:

答案 0 :(得分:3)

您在哪个路径存储pdf文件?

见下面一个从android Assets获取文件并将其复制到特定的SD卡位置。

我将首先检查pdf文件是否已经在欲望路径上可用。

 File file1 = new File("/sdcard/SampleProjectApp/WindsorONE_Mobile_Molding.pdf");
    File file2 = new File("/sdcard/SampleProjectApp/WindsorONE_Mobile_PK.pdf");
    File file3 = new File("/sdcard/SampleProjectApp/Alone.mp4");

    if(!((file1.exists())) || !((file2.exists())) || !((file3.exists()))) {
        ArrayList<String> files = new ArrayList<String>();
        files.add("WindsorONE_Mobile_Molding.pdf");         
        files.add("WindsorONE_Mobile_PK.pdf"); 
        files.add("Alone.mp4");
        new myAsyncTask().execute(files);
    }

现在,如果该文件不在该位置,那么它将执行myAsyncTask将文件从资产复制到SdCard。

 // AsyncTass for the Progress Dialog and to do Background Process
private class myAsyncTask extends AsyncTask<ArrayList<String>, Void, Void> {         
    ArrayList<String> files;         
    ProgressDialog dialog;         
    @Override         
    protected void onPreExecute() {             
        dialog = ProgressDialog.show(ListSample.this, "W1 SALES (beta)", "Loading...");         
    }         
    @Override         
    protected Void doInBackground(ArrayList<String>... params) {              
        files = params[0];             
        for (int i = 0; i < files.size(); i++) {                 
            copyFileFromAssetsToSDCard(files.get(i));                
        }             return null;         
    }         
    @Override         
    protected void onPostExecute(Void result) {             
        dialog.dismiss();         
    }      
} 



 // Function to copy file from Assets to the SDCard
public void copyFileFromAssetsToSDCard(String fileFromAssets){
    AssetManager is = this.getAssets();
    InputStream fis;
    try {

        fis = is.open(fileFromAssets);
        FileOutputStream fos;
        if (!APP_FILE_PATH.exists()) {
            APP_FILE_PATH.mkdirs();
        }
        fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/SampleProjectApp", fileFromAssets));
        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();
    }
}

<强>更新

对你而言:

替换  的 SampleProjectApp / WindsorONE_Mobile_Molding.pdf

<强>的Android /数据/ com.project.projectname /高速缓存/ YOUR_PDF_FILE.pdf

希望它有意义。

更新2

下面的代码是从一个路径到另一个路径的复制文件。您只需传递文件存在的路径以及必须复制的路径。

代码:

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)

你可以使用这种方法

boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if(isSDPresent)
{
    InputStream in = null;
    OutputStream out = null;
      in = //Place your file in the inputstream
      out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + filename);
      byte[] buffer = new byte[1024];
      int read;
      while((read = in.read(buffer)) != -1){
        out.write(buffer, 0, read);
      }
      in.close();
      in = null;
      out.flush();
      out.close();
      out = null;
}
else
{
Toast.makeText(getApplicationContext(), "SD Card not present", Toast.LENGTH_LONG).show();
}