0

我正在使用 Android,我正在尝试使用我已经拥有的数据库。我想把它放在SD卡上。我在项目的资产文件夹中有数据库文件。如何在安装了应用程序的任何设备的 SD 卡或外部存储上制作它?

4

4 回答 4

2
//Step1 : Checked accessiblity on sd card
public boolean doesSDCardAccessible(){
    try {
        return(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));        
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();        
    }   
    return false;
    }

//Step2 : create directory on SD Card
//APP_DIR : your PackageName 
public void createAndInitAppDir(){
    try {
    if(doesSDCardAccessible()){
    AppDir = new File(Environment.getExternalStorageDirectory(),APP_DIR+"/");
    if(!AppDir.exists()){
        AppDir.mkdirs();    
    }
    }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    }

//Step 3 : Create Database on sdcard
//APP_DIR : your PackageName 
//DATABASE_VERSION : give Database Vesrion
//DATABASE_NAME : your Databsename Name
public void initDB()
{
    try {

    //Using SQLiteHelper Class Created Database
    sqliteHelper = new SQLiteHelper(Application.this,AppDir.getAbsolutePath()+"/"+DATABASE_NAME, 
                                    null, DATABASE_VERSION);    

    //OR use following
    //Creating db here. or db will be created at runtime whenever writable db is opened.

    db = SQLiteDatabase.openOrCreateDatabase(AppDir.getAbsolutePath()+"/"+DATABASE_NAME,                                                     null);*/
    db= sqliteHelper.getWritableDatabase();
    db.close();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}
于 2013-04-24T05:03:10.413 回答
0

参考这个答案,您可以通过尝试以下两种方法在 Android 4.0+ 中找到 SD 卡的目录(每个设备只能工作一个):

new File("/mnt/external_sd/");

或者

new File("/mnt/extSdCard/");

在 Android <4.0 上,您可以使用

Environment.getExternalStorageDirectory();

你可以在那里创建你的 SQLite 数据库。此外,如果您找不到它,您可以遍历其中的所有目录/mnt/(注意:sdcard 将始终可以通过 访问/mnt/)。

于 2013-04-24T03:20:51.040 回答
0

数据库就像任何其他平面文件一样。只需将其复制到您的 SD 卡。

public boolean backup()
{
    File sdcard = Environment.getExternalStorageDirectory();
    File data = new File("/data/data/com.mydomain.myappname/databases/");

    if (sdcard.canWrite())
    {
    File input = new File(data, DB_NAME);
    File output = new File(sdcard, "android/data/com.mydomain.myappname/databases/");

    if(!output.exists())
    {
        if(output.mkdirs())
        {
            output = new File(sdcard,
            "android/data/com.mydomain.myappname/databases/backup.db");
            output.createNewFile();
            result = true;
        }
        else
        {
            output = new File(sdcard,
            "android/data/com.mydomain.myappname/databases/backup.db");
            result = true;
        }

        if(input.exists() && result)
        {
            FileInputStream source;
            FileOutputStream destination;

            try
            {
                source = new FileInputStream(input);
                    try
                    {
                        destination = new FileOutputStream(output);
                        byte[] buffer = new byte[1024];
                        int length;

                        while((length = source.read(buffer)) > 0)
                        {
                            destination.write(buffer, 0, length);
                        }

                        source.close();
                        destination.flush();
                        destination.close();
                        result = true;
                   }
                   catch(Exception e)
                   {
                       result = false;
                       destination = null;
                   }
               }
               catch(Exception e)
               {
                   result = false;
                   source = null;
               }
           }
           else
           {
               result = false;
           }
       }
       else
       {
           result = false;
       }
    }
    catch(Exception e)
    {
        result = false;
    }
    return result;
}
于 2013-04-24T03:29:20.770 回答
0

通过这个链接

或尝试关注

 InputStream myInput;

        try {

                     AssetManager assetManager = getAssets();
            myInput =  assetManager.open("mydatabase.db");


            File directory = new File("/sdcard/some_folder");

            if (!directory.exists()) {
                directory.mkdirs();
            }

            OutputStream myOutput = new FileOutputStream(directory
                    .getPath() + "/DatabaseSample.backup");

            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();

            myOutput.close();

            myInput.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
于 2013-04-24T04:36:28.190 回答