title: How to change the file/folder permission programmatically in android
body: <p><strong>I want to change the permission of a folder were application database is being stored.</strong></p>
  
  <p>After searching on Google I found this:-</p>
  
  <pre><code>public int chmod(File path, int mode) throws Exception {
    Class fileUtils = Class.forName("android.os.FileUtils");
    Method setPermissions =
        fileUtils.getMethod("setPermissions", String.class, int.class, int.class, int.class);
    return (Integer) setPermissions.invoke(null, path.getAbsolutePath(), mode, -1, -1);
  }
  
  ...
  chmod("/foo/bar/baz", 0755);
  ...
  </code></pre>
  
  <p>Here the <code>FileUtils</code> class should be a part of <code>android.os</code> package but I wasn't able to find it while importing the package.</p>
  
  <p><em>Any idea how to use <code>FileUtils</code> or some other way to change the permission of folder?</em></p>
  
  <p><strong>I am changing the permission because after installing my app when I try to access the database, app stops working. As my device is <code>rooted</code> so when I change the permission of the database folder from <code>771</code> to <code>777</code> using <code>Root Browser</code> the app starts working perfectly.</strong></p>
  
  <p><em>So how to change the permission of the folder while installation of app i.e. programmatically in <code>unrooted</code> devices?</em> </p>
  
  <p>My <code>DBHelper</code> class:-</p>
  
  <pre><code>public class DBHelper extends SQLiteOpenHelper {
  
  public SQLiteDatabase database = null;
  public File databaseFile;
  public static String databaseName = "Db1.sqlite";
  public String databasePath = "";
  Context mContext;
  
  public DBHelper(Context paramContext) {
  
      super(paramContext, databaseName, null, 1);
      this.mContext = paramContext;
  
      Log.d("data", "package name:" + paramContext.getPackageName());
  
      //this.databasePath = ("data/data/" + paramContext.getPackageName() + "/databases/"+databaseName);
      this.databasePath = (Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android/data/"+paramContext.getPackageName()+"/"+databaseName);
      System.out.println(databasePath);
      this.databaseFile = new File(this.databasePath);
      if (!this.databaseFile.exists())
          try {
              deployDataBase(DBHelper.databaseName, this.databasePath);
              return;
          } catch (IOException localIOException) {
              localIOException.printStackTrace();
          }
  }
  
  private void deployDataBase(String dbNAme, String dbPath)
          throws IOException {
      InputStream localInputStream = this.mContext.getAssets().open(dbNAme);
      FileOutputStream localFileOutputStream = new FileOutputStream(dbPath);
      byte[] arrayOfByte = new byte[1024];
      while (true) {
          int i = localInputStream.read(arrayOfByte);
          if (i &lt;= 0) {
              localFileOutputStream.flush();
              localFileOutputStream.close();
              localInputStream.close();
              return;
          }
          localFileOutputStream.write(arrayOfByte, 0, i);
      }
  }
  
  @Override
  public synchronized void close() {
  
      if (database != null)
          database.close();
  
      super.close();
  
  }
  @Override
  public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
  
  }
  </code></pre>