我想在sqlite中保存3gp文件,而不是3gp文件的路径。 1.是否可以在sqlite中保存3gp文件? 2.怎么做? 3.如果是,创建后会产生哪种类型的问题?
修:
我可以使用blob将图像文件保存到sqlite中。对于图像,我正在从“位图转换为字节数组”进行写入并从“字节数组转换为位图”进行读取。但对于3gp文件,我无法写入和读取它。
答案 0 :(得分:3)
试试这段代码: -
private String downloadVideo(String url)
{
String video = "";
URL u = null;
InputStream is = null;
try {
u = new URL(url);
is = u.openStream();
HttpURLConnection huc = (HttpURLConnection)u.openConnection();//to know the size of video
if(huc != null)
{
InputStream inputStream = huc.getInputStream();
byte[] buff = new byte[8000];
int bytesRead = 0;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while((bytesRead = inputStream.read(buff)) != -1) {
bao.write(buff, 0, bytesRead);
}
byte[] data = bao.toByteArray();
video = Base64.encodeBytes(data);
}
}catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if(is != null){
is.close();
}
}catch (IOException ioe) {
}
}
return video;
}
您可以将字符串存储到数据库中。从数据库获取时,您可以将字符串解码为输入流并获取真实视频。
http://www.helloandroid.com/tutorials/store-imagesfiles-database
但是将视频存储到数据库中并不是一个好主意,它会增加数据库的大小和查询的执行时间。
了解更多信息: -
答案 1 :(得分:0)
是否可以在sqlite中保存3gp文件?
你有答案,BLOB。
怎么做?
正如bashu回答的那样,使用Base64是一个很好的想法来做数据流。使用base64可以方便地将数据保存到数据库中。
如果是,创建后会产生哪种类型的问题?
这是您提出的最佳问题片段。 是的,它会。不要这样做。而是保留文件路径的字符串值并使用它们。
原因:
1。不必要地增加了数据库的负担
2。光标对象会给你异常,因为当涉及到小型设备时,设备内存对于保存在你的记忆中非常重要。(Unless you are planning to keep images all together of size less than 1MB)