我想更改应用程序数据库存储时文件夹的权限。
在Google上搜索后,我发现了这一点: -
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
}
答案 0 :(得分:2)
不要使用您的案例的数据库路径和文件权限。
IMO Root和普通设备是互斥的受众。
仅对于您的测试有根设备,使用从Environment.getExternalStorage **方法派生的路径在SD卡上创建db。
让SD卡上的数据库。做这样的事情。
将此类放入代码包中。
package com.test.db;
import java.io.File;
import android.content.Context;
import android.content.ContextWrapper;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.os.Environment;
/**
* Custom context wrapper to proxy the database file path.
* Instead of storing data on internal storage for app
* we will storing the data on external sd card.
*/
public class DatabaseContext extends ContextWrapper {
/*
* Used for logging.
*/
private static final String DEBUG_CONTEXT = "DatabaseContext";
/*
* Constructor wrapping given app context.
*/
public DatabaseContext(Context base) {
super(base);
}
/**
* Proxy the path to data base file for every operation
* @see android.content.ContextWrapper#getDatabasePath(java.lang.String)
*/
@Override
public File getDatabasePath(String name) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
File sdcard = Environment.getExternalStorageDirectory();
String dbfile = sdcard.getAbsolutePath() + File.separator + "com.test" + File.separator + "database" + File.separator
+ name;
if (!dbfile.endsWith(".db")) {
dbfile += ".db";
}
File result = new File(dbfile);
if (!result.getParentFile().exists()) {
if(!result.getParentFile().mkdirs()){
result = new File(sdcard, name);
}
}
if (android.util.Log.isLoggable(DEBUG_CONTEXT, android.util.Log.WARN)) {
android.util.Log.w(DEBUG_CONTEXT,
"getDatabasePath(" + name + ") = " + result.getAbsolutePath());
}
return result;
} else {
// Something else is wrong. It may be one of many other states,
// Writing data on internal storage
return super.getDatabasePath(name);
}
}
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode,
CursorFactory factory) {
return openOrCreateDatabase(name, mode, factory, null);
}
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
DatabaseErrorHandler errorHandler) {
SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name)
.getAbsolutePath(), null, errorHandler);
return result;
}
}
像这样扩展SqliteOpenHelper类
package com.test.db;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Custom SqliteOpenHelper.
*/
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static final String TAG = My.class.getSimpleName();
private final Context context;
public MySQLiteOpenHelper(Context context, String name, int version, String mResourceName) {
super(new DatabaseContext(context), name, /*factory*/null, version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create tables here
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
这是经过测试的片段,用于在SD卡中创建数据库。请检查