Android 如何获取数据库路径和名称

Android 如何获取数据库路径和名称,android,database,sqlite,path,Android,Database,Sqlite,Path,我可以用这个来获取数据库路径和名称吗 Paths.getDbPath(); Paths.getDbName(); 我想如果我们在应用程序中使用数据库,我们可以将其存储在assets文件夹中并重命名吗?我不熟悉路径,因此无法回答您的问题。 但我可以告诉您如何将数据库复制到资产 DBHelper中的方法 public void exportDatabase(String destin) throws IOException { File sourceFile = new File(ge

我可以用这个来获取数据库路径和名称吗

Paths.getDbPath();

Paths.getDbName();

我想如果我们在应用程序中使用数据库,我们可以将其存储在assets文件夹中并重命名吗?

我不熟悉路径,因此无法回答您的问题。 但我可以告诉您如何将数据库复制到资产

DBHelper中的方法

public void exportDatabase(String destin) throws IOException {

    File sourceFile = new File(getReadableDatabase().getPath());    
    File destinationDir = new File(Environment.getExternalStorageDirectory(), "DBNAME");
    FileUtils.copyFileToDirectory(sourceFile, destinationDir);


    String sourcetemp = "source.db";
    String destintemp = destin + ".db";
    File from = new File(destinationDir, sourcetemp);
    File to = new File(destinationDir, destintemp);
    try {
        from.renameTo(to);
    }
    catch (Exception e) {
        Log.e(TAG, "Rename failed", e);
    }
}   

希望对您有所帮助。

您可以使用以下代码找到数据库路径:

    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
{
              DB_PATH = context.getApplicationInfo().dataDir + "/databases/";         
            }
            else
            {              
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
            }

“Paths”不是Android框架中的一个类,因此如果您不告诉我们它是从哪里来的,就无法回答您的问题。private static String DB_PATH=Paths.getDbPath();私有静态字符串DB_NAME=path.getDbName();我在一个项目中看到了这行代码,但您仍然没有告诉我们从何处获得该类。这是我的代码私有void getInfoFromDB(int id){SQLiteDatabase sql=null;Cursor cur=null;Cursor cur1=null;int no_of_rows=0;试试{sql=SQLiteDatabase.openDatabase(Paths.geDBPath()+Paths.getDbName(),null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);}catch(SQLiteException e){//TODO:handleexception}k谢谢..如果我们将数据库存储在assets文件夹中,那么我们如何在代码中检索它。上述方法有效吗???