Java 从文件在SD卡上创建数据库

Java 从文件在SD卡上创建数据库,java,android,sqlite,android-sdcard,Java,Android,Sqlite,Android Sdcard,我正在使用安卓系统,我正在尝试使用我已有的数据库。我想把它放在SD卡上。我在项目的资产文件夹中有数据库文件。我如何在SD卡或安装应用程序的任何设备的外部存储器上运行该应用程序?在参考中,您可以通过尝试以下两种方法在Android 4.0+中找到SD卡的目录(每个设备只能运行一个): 或 在安卓上,数据库与任何其他平面文件一样。把它复制到你的SD卡上 public boolean backup() { File sdcard = Environment.getExternalStorage

我正在使用安卓系统,我正在尝试使用我已有的数据库。我想把它放在SD卡上。我在项目的资产文件夹中有数据库文件。我如何在SD卡或安装应用程序的任何设备的外部存储器上运行该应用程序?

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


在安卓上,数据库与任何其他平面文件一样。把它复制到你的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;
}
通过这个

或者试试下面的

 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();
        }

我可以从SD卡访问它吗?访问什么?数据库?到底是什么不起作用?如果你不描述问题,当你没有得到答案时,不要感到惊讶。当然。我只是自己搞不清楚到底发生了什么。出于某种原因,我在这里找到的DataBaseHelper类:拒绝创建数据库…即使我能够在其他设备上创建它。我是否需要某些权限才能在代码中指定的位置创建数据库?这实际上很奇怪。我可以很好地运行createDatabase()和openDatabase(),以及getReadableDatabase()。但是当我试图查询它时,它声称列不存在…所以数据库没有正确创建或者根本没有创建。但是…为什么它不返回一个错误呢?
Environment.getExternalStorageDirectory();
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;
}
 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();
        }
//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();
    }
}