在android中将数据库从服务器下载和复制到资产文件夹


Downloading & copying database from server to assets folder in android

我想从PHP(server)下载SQLITE数据库并将其复制到Android(client)的资产文件夹中。我没有得到所需的输出。谁能告诉我我在哪里犯了错误或错过了什么?感谢您的任何帮助。

法典:

    boolean downloadDatabase(Context context) {
    try {
           Log.d("test", "downloading database");
            URL url = new URL("http://10.0.2.2/SvcEnggPlanner/" + "Services.sqlite");
            URLConnection ucon = url.openConnection();
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
            }
           FileOutputStream fos = null;
            fos = context.openFileOutput("Services.sqlite", Context.MODE_PRIVATE); 
            if(fos!=null){
            fos.write(baf.toByteArray());
            fos.close();
           Log.d("test", "downloaded");
            }
    } catch (IOException e) {
            Log.e("test", "downloadDatabase Error: " , e);
            return false;
    }  catch (NullPointerException e) {
            Log.e("test", "downloadDatabase Error: " , e);
            return false;
    } catch (Exception e){
            Log.e("test", "downloadDatabase Error: " , e);
            return false;
    }
    return true;
}
public void copyServerDatabase() {
    SQLiteDatabase db = getReadableDatabase();
    db.close();
        OutputStream os = null;
        InputStream is = null;
       try {
                Log.d("test", "Copying DB from server version into app");
                is = context.openFileInput("Services.sqlite");
                Log.d("test", ""+is);
                os = new FileOutputStream("/data/data/com.sever/databases/");
                Log.d("test", ""+os);
                copyFile(os, is);
       } catch (Exception e) {
           Log.d("test", "Server Database was not found - did it download correctly?");                          
       } 
finally {
                try {
                        if(os != null){
                                os.close();
                        }
                        if(is != null){
                                is.close();
                        }
                } catch (IOException e) {
                        Log.d("test", "failed to close databases");
                }
       }
       Log.d("test", "Done Copying DB from server");
}
    private void copyFile(OutputStream os, InputStream is) throws IOException {
    Log.d("test", "In CopyFile");
       byte[] buffer = new byte[1024];
       int length;
       while((length = is.read(buffer))>0){
            os.write(buffer, 0, length);
       }
       os.flush();
 }
 }

您无法在 Android 应用程序的 assets 文件夹中写入文件,因为它不可写。安卓apk的文件只能读,不能写。