通过Android应用程序将png图像保存在数据库中 [英] Saving png image in database through android app

查看:64
本文介绍了通过Android应用程序将png图像保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用开发的应用程序将图像保存到移动设备中,什么是最好的方法.

我试图通过sqlite保存,但是还有其他选择吗?

解决方案

推荐的方法是将图像存储为文件而不是存储在数据库中,然后存储路径或足够的路径以在数据库中唯一标识图像

要存储图像,您必须基于byte [](字节数组)将其存储为BLOB;但是,使用Android SDK存在一个局限,即使可以保存小于2MB的图像,也只能对其进行检索.

  • 从技术上讲,您可以将图像存储为十六进制,二进制,八进制字符串,但这会更复杂,效率更低甚至更具限制性..因此,实际上您必须将其存储为BLOB 并不完全是事实.

使用SQLite或大多数结构化的数据库存储图像(或任何大文件)实际上实际上是无用的,因为在数据查询方面您几乎无能为力.

您实际上是通过SQL来保存BLOB的:-

  INSERT INTO your_table VALUES(x'00FF01EF'); 

  • 请注意,以上内容仅适用于具有单列的表

但是,SQLiteDatabase 插入便捷方法代表您进行了从byte []到正确的SQL(十六进制字符串)的转换.

所以您通常会使用:-

  ContentValues cv = new ContentValues();cv.put("your_column",your_image_as_a_byte_array);your_sqlitedatabase_object.insert("your_table",null,cv); 

使用类似

的内容从游标(查询的结果)中检索字节数组.

  your_retrieved_image_byte_array = your_cursor.getBlob(your_column_offset_as_an_int);//<<<<<<<<如果该行无法容纳2 MB的限制的CursorWindow,则CursorWindow完全错误 

您不妨查看如何在sqlite数据库中插入图像,因为这会更详细,并且具有存储图像和路径的代码(要么根据图像大小存储图像(小于100k,然后存储图像),否则将存储路径),并另外将图像检索到ListView中.此外,尽管不建议这样做,但这是一种绕过2Mb限制的方法 解决方案

The recommended way is to store the image as a file not in the database and then store the path or enough of the path to uniquely identify the image in the database.

To store an image you have to store it as a BLOB based upon a byte[] (byte array); However, using the Android SDK, there is a limitation that only images less than 2MB can be retrieved even though they can be saved.

  • Technically you could store the image as a hex, binary, octal string but that would be more complex, less efficient and even more restrictive.. So really you have to store it as a BLOB is not completely factual.

It's also actually pretty useless storing images (or any large files) with SQLite or most structured databases as there is little that you can do with the data query wise.

You actually save a BLOB via SQL using something like :-

INSERT INTO your_table VALUES(x'00FF01EF');

  • i.e. 00 (0) is the first byte, FF (255) the 2nd, 01 (01) the 3rd .........
  • note the above would only work for a table with a single column

However, the SQLiteDatabase insert convenience method does the conversion from the byte[] to the correct SQL (hex string) on your behalf.

So you'd typically use :-

ContentValues cv = new ContentValues();
cv.put("your_column",your_image_as_a_byte_array);
your_sqlitedatabase_object.insert("your_table",null,cv);

You retrieve the byte array from a Cursor (result of a query) using something along the lines of

your_retrieved_image_byte_array = your_cursor.getBlob(your_column_offset_as_an_int); //<<<<<<<<< CursorWindow full error if the row cannot fit into the CursorWindow which has 2MB limit

You may wish to have a look at How can I insert image in a sqlite database, as this goes into more detail and has code that stores both images and paths (either one is stored based upon the image size, less then 100k, then the image is stored, else the path is stored) and additionally retrieves the images into a ListView. Additionally, although not recommended this is a way of getting around the 2Mb limit How to use images in Android SQLite that are larger than the limitations of a CursorWindow?

这篇关于通过Android应用程序将png图像保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆