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

The database is like any other flat file. Just copy it to your SD card.

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 に答える
0

この回答を参照して、次の両方を試すことで、Android 4.0 以降の SD カードのディレクトリを見つけることができます (デバイスごとに 1 つだけが機能するはずです)。

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

また

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

Android <4.0では、使用できます

Environment.getExternalStorageDirectory();

そこに SQLite データベースを作成できます。さらに、見つからない場合は、すべてのディレクトリを反復処理できます/mnt/(注: sdcard は常に経由でアクセスできます/mnt/)。

于 2013-04-24T03:20:51.040 に答える