将文件从内部存储到外部存储 [英] Copy Files from Internal Storage to external storage

查看:139
本文介绍了将文件从内部存储到外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去使用Adobe Reader来从服务器读取的问题是,当我将其存储在内部存储的其他应用程序无法读取该文件下载的PDF文件。现在,我想知道我怎么能复制这个文件中的外部存储(/ SD卡/),因此它可以通过PDF浏览器中查看。

由于安全原因,我存储在内部存储的文件,并会删除一个在外部存储之后。

我的问题是我怎么可以复制保存在内部存储器中的文件,而无需使用原始或资产放在输入流。

 的InputStream myInput = getResources()openRawResource(R.raw.p13)。

        FileOutputStream中FOS = openFileOutput(样本,Context.MODE_PRIVATE);

        //传输的字节从输入文件到输出文件
        byte []的缓冲区=新的字节[1024];
        INT长;
        而((长度= myInput.read(缓冲液))大于0)
        {
            fos.write(缓冲液,0,长度);
        }
        //关闭流
        fos.flush();
        fos.close();
        myInput.close();
 

解决方案

在该路径要存储的PDF文件?

请参阅下面的一个来从Android的资产文件,并将其复制到SD卡的具体位置。

我会首先检查PDF文件是否已经可以在欲望的路径或没有。

 文件文件1 =新的文件(/ SD卡/ SampleProjectApp / WindsorONE_Mobile_Molding.pdf);
    文件文件2 =新的文件(/ SD卡/ SampleProjectApp / WindsorONE_Mobile_PK.pdf);
    文件文件3 =新的文件(/ SD卡/ SampleProjectApp / Alone.mp4);

    如果(!((file1.exists()))||!((file2.exists()))||!((file3.exists()))){
        ArrayList的<字符串>文件=新的ArrayList<字符串>();
        files.add(WindsorONE_Mobile_Molding.pdf);
        files.add(WindsorONE_Mobile_PK.pdf);
        files.add(Alone.mp4);
        新myAsyncTask()执行(文件);
    }
 

现在,如果该文件不是present在那个位置,然后它会执行从资产复制文件的SD卡的myAsyncTask。

  // AsyncTass的进程对话框,并做后台进程
私有类myAsyncTask扩展的AsyncTask< ArrayList的<字符串>中虚空,虚空> {
    ArrayList的<字符串>文件;
    ProgressDialog对话框;
    @覆盖
    在preExecute保护无效(){
        对话框= ProgressDialog.show(ListSample.this,W1销售(测试版),载入中...);
    }
    @覆盖
    保护无效doInBackground(ArrayList中<字符串> ... PARAMS){
        文件= PARAMS [0];
        的for(int i = 0; I< files.size();我++){
            copyFileFromAssetsToSDCard(files.get(ⅰ));
        }返回null;
    }
    @覆盖
    保护无效onPostExecute(无效的结果){
        dialog.dismiss();
    }
}



 //函数从资产复制文件到SD卡
公共无效copyFileFromAssetsToSDCard(字符串fileFromAssets){
    AssetManager是= this.getAssets();
    InputStream的FIS;
    尝试 {

        FIS = is.​​open(fileFromAssets);
        FileOutputStream中FOS;
        如果(!APP_FILE_PATH.exists()){
            APP_FILE_PATH.mkdirs();
        }
        FOS =新的FileOutputStream(新文件(Environment.getExternalStorageDirectory()+/ SampleProjectApp,fileFromAssets));
        byte []的B =新的字节[8]。
        INT I;
        而((ⅰ= fis.read(二))!=  -  1){
            fos.write(B,0,I);
        }
        fos.flush();
        fos.close();
        fis.close();
    }
    赶上(IOException异常E1){
        e1.printStackTrace();
    }
}
 

更新

有关你的一部分:

替换   SampleProjectApp / WindsorONE_Mobile_Molding.pdf

的Andr​​oid /数据/ com.project.projectname /缓存/ YOUR_PDF_FILE.pdf

希望这是有意义的。

更新2

下面code是一个路径到另一条路径复制文件。您只需通过的文件在那里存在的路径和它必须是副本。路径

code:

 公共静态布尔的CopyFile(String一个,字符串){
尝试 {
    INT bytesum = 0;
    INT byteread = 0;
    文件的oldfile =新的文件(从);
    如果(oldfile.exists()){
        的InputStream inStream中=新的FileInputStream(从);
        FileOutputStream中FS =新的FileOutputStream(到);
        byte []的缓冲区=新的字节[1444];
        而((byteread = inStream.read(缓冲液))!=  -  1){
            bytesum + = byteread;
            fs.write(缓冲液,0,byteread);
        }
        inStream.close();
        fs.close();
    }
    返回true;
}赶上(例外五){
    返回false;
}
 

}

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

解决方案

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

}

这篇关于将文件从内部存储到外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆