2016-09-20 30 views
-5

我在安卓APK,每当用户打开其为具有文件名列表中的应用程序,如果他点击相应文件的文件名,从资产文件夹打开的资产文件夹中保存的一些文本文件,如何将android应用资产文件夹中的文本文件保存为android手机的本地目录作为文本或pdf文件?

现在我想让用户在文本或PDF格式的文件保存到自己的本地目录,以便他可以根据自己的选择

编辑转让或使用它或修改它:我只是想知道的API所必需的,而不是整个代码

+1

显示的东西你都试过,而不是直接要求代码。 –

回答

0

使用此功能将资产中的内容复制到存储区

private void copyAssets() { 
     AssetManager assetManager = getAssets(); 
     String[] files = null; 
     try { 
      files = assetManager.list(""); 
     } catch (IOException e) { 
      Log.e("tag", "Failed to get asset file list.", e); 
     } 
     if (files != null) for (String filename : files) { 
      InputStream in = null; 
      OutputStream out = null; 
      try { 
       in = assetManager.open(filename); 
       File outFile = new File(getExternalFilesDir(null), filename); 
       out = new FileOutputStream(outFile); 
       copyFile(in, out); 
      } catch(IOException e) { 
       Log.e("tag", "Failed to copy asset file: " + filename, e); 
      }  
      finally { 
       if (in != null) { 
        try { 
         in.close(); 
        } catch (IOException e) { 
         // NOOP 
        } 
       } 
       if (out != null) { 
        try { 
         out.close(); 
        } catch (IOException e) { 
         // NOOP 
        } 
       } 
      } 
     } 
    } 
    private void copyFile(InputStream in, OutputStream out) throws IOException { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while((read = in.read(buffer)) != -1){ 
      out.write(buffer, 0, read); 
     } 
    } 

参考:Move file using Java