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

我试图使用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(); 

您在哪个路径存储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 files = new ArrayList(); 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, Void, Void> { ArrayList files; ProgressDialog dialog; @Override protected void onPreExecute() { dialog = ProgressDialog.show(ListSample.this, "W1 SALES (beta)", "Loading..."); } @Override protected Void doInBackground(ArrayList... 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

安卓/数据/ 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; } 

}

你可以使用这种方法

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