复制SD卡上的现有数据库

时间:2012-04-20 09:37:59

标签: android database copy

当我运行android应用程序时,它将数据库直接存储在模拟器的内部存储器中。应用程序(.apk文件)存储在外部存储器中。数据库的大小为230MB。我的manifest.xml中有以下几行:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<manifest android:installLocation="preferExternal" >

以及导致错误的以下代码INSTALL_FAILED_DEXOPT:

public void copyDatabase() throws IOException 
    {
        if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
        {
            Toast.makeText(mycontext,"External Sd card not mounted", Toast.LENGTH_LONG).show();
        }
        try
        {
            InputStream in=mycontext.getAssets().open("Demo.db");
            File outFile =new File(Environment.getExternalStorageDirectory()+File.separator+ "Demo.db);
            outFile.createNewFile();
            OutputStream out= new FileOutputStream(outFile);
            byte[] buf = new byte[1024];
            int len;
            while((len = in.read(buf))> 0)
            {
                out.write(buf,0,len);
            }
            out.close();
            in.close();
            Log.i("copyDatabase","Database Has been transferred");

        }
        catch(IOException e)
        {
            Log.i("CopyDatabase","could not copy database");
        }

    }
    public  boolean checkDatabase() 
    {   
        SQLiteDatabase checkdb=null;
    try
    {
        String myPath="/sdcard/demo.db";
        checkdb=SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        Log.i("checkDatabase database path",checkdb.getPath());
    }
    catch(SQLiteException e)
    {
        Log.e("Database doesn`t exist",e.toString());
    }
    if(checkdb!=null)
    {
        checkdb.close();
    }
    return checkdb != null ? true : false;


    }
    public void openDatabase() throws SQLException
    {
        String myPath="/sdcard/demo.db"
        myDatabase=SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

0 个答案:

没有答案