Copy Files from Internal Storage to external stora

2019-04-13 21:58发布

Im trying to use Adobe reader to read pdf files downloaded from the server the problem is When i store it in the internal storage Other application can't read the file. Now i Want to know how can i Copy this file in the external storage(/sdcard/) so it can be viewed by pdf viewers.

Due to security reason I'm storing the files in the Internal Storage and would delete the one in external storage afterwards.

My question is How can i copy the file saved in the internal storage without using the raw or the assets to put in the inputstream.

    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条回答
祖国的老花朵
2楼-- · 2019-04-13 22:28

you can use this method

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();
}
查看更多
Explosion°爆炸
3楼-- · 2019-04-13 22:35

at which path you are storing the pdf file ?

See below one to get file from android Assets and copy it to the specific sdcard location.

I will first check for the pdf file whether it is already available at desire path or not.

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

Now, if the file is not present at that position then it will execute the myAsyncTask to copy file from assets to the 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();
    }
}

Updated

For your part:

replace SampleProjectApp/WindsorONE_Mobile_Molding.pdf

with

Android/data/com.project.projectname/cache/YOUR_PDF_FILE.pdf

Hope it make sense.

Updated 2

Below code is copy file from one path to another path. You simply have to pass the path of the file where it is exist and the path where it has to be copy.

Code:

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

}

查看更多
登录 后发表回答