title: How can i copy app data to sdcard in android programatically?
body: <p>I am doing an app to backup and restore all user installed apps in android device. I am able to backup apk files of all apps that user selected to backup. Now i have to backup the data files of the app along with apk file.</p>
  
  <p>i am trying something like this</p>
  
  <pre><code>public void copyDbToSdcard(String packageName) {
      InputStream myInput;
      String dbpath = "/data/"+packageName+"/databases/";
      String sdpath = Environment.getExternalStorageDirectory().getPath();
  
      try {
  
          File exist=new File(Environment.getDataDirectory()+dbpath);
          System.out.println("1 "+ exist.exists());
  
          // Set the output folder on the Scard
          File directory = new File(sdpath + "/Back");
          // Create the folder if it doesn't exist:
          if (!directory.exists()) {
              directory.mkdirs();
          }
          // Set the output file stream up:
          myInput = new FileInputStream(Environment.getDataDirectory()
                  + dbpath);
  
          OutputStream myOutput = new FileOutputStream(directory.getPath()
                  + "/refuel_db");
  
          // Transfer bytes from the input file to the output file
          byte[] buffer = new byte[1024];
          int length;
          while ((length = myInput.read(buffer)) &gt; 0) {
              myOutput.write(buffer, 0, length);    
          }
          // Close and clear the streams
  
          myOutput.flush();
  
          myOutput.close();
  
          myInput.close();
  
          Toast.makeText(MainActivity.this, "Backup Done Succesfully!", Toast.LENGTH_LONG)
                  .show();
  
      } catch (FileNotFoundException e) {
  
  
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
  
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I am getting error log as</p>
  
  <p><code>EACCES (Permission denied)</code> on line  </p>
  
  <p><code>myInput = new FileInputStream(Environment.getDataDirectory()+ dbpath);</code></p>
  
  <p>I already gave READ and WRITE permissions in manifest file</p>