【发布时间】:2014-10-13 09:07:15
【问题描述】:
如果正在存储应用程序数据库,我想更改文件夹的权限。
在谷歌上搜索后发现:-
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,所以当我使用Root Browser 将数据库文件夹的权限从771 更改为777 时,应用程序开始完美运行。
那么如何在安装应用程序时更改文件夹的权限,即在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
}
【问题讨论】:
-
你在玩一个危险的游戏。
-
@SotiriosDelimanolis 但是如果我不这样做,我该如何解决我的问题?
-
你的数据库没有在外部 sdcard 中定义?
-
@Javanator 如果设备没有外部 sdcard 怎么办?
-
现在的大多数设备都没有外部存储插槽,但有外部存储作为内部存储的模拟分区
标签: java android file-permissions chmod fileutils