I want to change the permission of a folder were application database is being stored.
After searching on Google I found this:-
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);
...
Here the FileUtils
class should be a part of android.os
package but I wasn't able to find it while importing the package.
Any idea how to use FileUtils
or some other way to change the permission of folder?
I am changing the permission because after installing my app when I try to access the database, app stops working. As my device is rooted
so when I change the permission of the database folder from 771
to 777
using Root Browser
the app starts working perfectly.
So how to change the permission of the folder while installation of app i.e. programmatically in unrooted
devices?
My DBHelper
class:-
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 <= 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
}
"data/data/" + paramContext.getPackageName() + "/databases/"+databaseName
path to store thedb
. – Fsh