android 如何打开s3db,How to download an SQLite database from an Android device?

这段代码展示了如何从指定URL下载SQLite数据库文件,并将其存储在Android应用的私有存储中。首先,通过打开连接并读取输入流来下载数据库文件,然后将字节写入到应用的文件输出流中。之后,使用`copyServerDatabase`方法将下载的文件复制到系统的数据库路径,以便应用可以访问和处理。确保在AndroidManifest.xml中添加了互联网权限。
摘要由CSDN通过智能技术生成

Here's my implementation:

private static boolean downloadDatabase(Context context) {

try {

// Log.d(TAG, "downloading database");

URL url = new URL("http://some-url.com/db/" + "db_name.s3db");

/* Open a connection to that URL. */

URLConnection ucon = url.openConnection();

/*

* Define InputStreams to read from the URLConnection.

*/

InputStream is = ucon.getInputStream();

BufferedInputStream bis = new BufferedInputStream(is);

/*

* Read bytes to the Buffer until there is nothing more to read(-1).

*/

ByteArrayBuffer baf = new ByteArrayBuffer(50);

int current = 0;

while ((current = bis.read()) != -1) {

baf.append((byte) current);

}

/* Convert the Bytes read to a String. */

FileOutputStream fos = null;

// Select storage location

fos = context.openFileOutput("db_name.s3db", Context.MODE_PRIVATE);

fos.write(baf.toByteArray());

fos.close();

// Log.d(TAG, "downloaded");

} catch (IOException e) {

Log.e(TAG, "downloadDatabase Error: " , e);

return false;

} catch (NullPointerException e) {

Log.e(TAG, "downloadDatabase Error: " , e);

return false;

} catch (Exception e){

Log.e(TAG, "downloadDatabase Error: " , e);

return false;

}

return true;

}

This downloads the database into your applications private storage see here

Don't forget you need the internet permission in your manifest.

To then get an actual database from this file you can do this:

/**

* Copies your database from your local downloaded database that is copied from the server

* into the just created empty database in the

* system folder, from where it can be accessed and handled.

* This is done by transfering bytestream.

* */

private void copyServerDatabase() {

// by calling this line an empty database will be created into the default system path

// of this app - we will then overwrite this with the database from the server

SQLiteDatabase db = getReadableDatabase();

db.close();

OutputStream os = null;

InputStream is = null;

try {

// Log.d(TAG, "Copying DB from server version into app");

is = mContext.openFileInput("db_name.s3db");

os = new FileOutputStream("/data/data/your.package.name/databases/"); // XXX change this

copyFile(os, is);

} catch (Exception e) {

Log.e(TAG, "Server Database was not found - did it download correctly?", e);

} finally {

try {

//Close the streams

if(os != null){

os.close();

}

if(is != null){

is.close();

}

} catch (IOException e) {

Log.e(TAG, "failed to close databases");

}

}

// Log.d(TAG, "Done Copying DB from server");

}

private void copyFile(OutputStream os, InputStream is) throws IOException {

byte[] buffer = new byte[1024];

int length;

while((length = is.read(buffer))>0){

os.write(buffer, 0, length);

}

os.flush();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值