如果正在存储应用程序数据库,我想更改文件夹的权限。
在谷歌上搜索后,我发现了这个:-
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);
...
这里的FileUtils
类应该是android.os
包的一部分,但我在导入包时找不到它。
知道如何使用FileUtils
或其他方式来更改文件夹的权限吗?
我正在更改权限,因为在我尝试访问数据库时安装我的应用程序后,应用程序停止工作。正如我的设备一样rooted
,当我将数据库文件夹的权限从使用该应用程序更改771
为开始正常工作时。777
Root Browser
那么如何在设备中以编程方式安装应用程序时更改文件夹的权限unrooted
?
我的DBHelper
班级:-
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
}