title: Saving Files from Assets folder to device storage working only the first time! Android studio
body: <p>I developing an Android app on Android Studio.
  I added a few files on Assets folder and my goal is the user through a button press to save them on his device.
  I managed to find a way to do that but I have a problem!</p>
  <p>After the first installation the app runs perfectly!
  When I uninstalled it and reinstalled it the files were failing to be saved!</p>
  <p>As you can see on my code, the folder that I am saving the files is the &quot;/Documents&quot;.
  I tried to change this location on my code and install the app again. Same results!!
  It runs for the first time but when I am unistall it and reinstall it (with no changes on my code) I am getting the &quot;Failed!&quot; message as you can see on my code!</p>
  <p>Do you guys have any idea what am I missing here?</p>
  <p>Thanks!!</p>
  <pre><code>public class GKL10_pre extends AppCompatActivity {
  
  
      private Button button; 
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_gkl10_pre);
  
  
  
  
          button = (Button) findViewById(R.id.B_GKL10_FILES);
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  copyAsset(&quot;file1.pdf&quot;);
                  copyAsset(&quot;file2.pdf&quot;);
                  copyAsset(&quot;file3.pdf&quot;);
                  copyAsset(&quot;file4.pdf&quot;);
  
              }
          });
      }
  
      private void copyAsset(String filename){
          String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + &quot;/Documents&quot;;
          File dir = new File(dirPath);
          if(!dir.exists()) {
              dir.mkdirs();
          }
          AssetManager assetManager = getAssets();
          InputStream in = null;
          OutputStream out = null;
          try {
              in = assetManager.open(filename);
              File outFile = new File(dirPath, filename);
              out = new FileOutputStream(outFile);
              copyFile(in, out);
              Toast.makeText(this, &quot;Saved!&quot;, Toast.LENGTH_SHORT).show();
          } catch (IOException e) {
              e.printStackTrace();
              Toast.makeText(this, &quot;Failed!&quot;, Toast.LENGTH_SHORT).show();
          } finally {
              if(in != null){
                  try {
                      in.close();
                  } catch (IOException e){
                      e.printStackTrace();
                  }
              }
              if(out != null){
                  try {
                      out.close();
                  } catch (IOException e){
                      e.printStackTrace();
                  }
              }
          }
      }
  
      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);
          }
      }
  
  
  }```
  </code></pre>