简体   繁体   English

如何将图像和字符串存储在同一数据库表中并检索它们

[英]How can I store images and strings in the same database table and retrieve them

I have a form that accepts texts and images and I want to store the data in the same database table. 我有一个接受文本和图像的表单,我想将数据存储在同一数据库表中。 Below is the declaration for my record but they are all strings so how do i convert the images to strings to add to this list: 以下是我的记录的声明,但它们都是字符串,因此如何将图像转换为字符串以添加到此列表中:

site_info_record.createSiteInfoRecord(sql_sysaid_id, sql_site_id, sql_link_id, sql_customer_name, sql_site_contact,
            sql_task_type, sql_address, sql_region, sql_phone, sql_fax, sql_mobile, sql_email,
            sql_landlord_name, sql_rent_status, sql_loc_cords, sql_power_status, sql_voltage, sql_aircon, sql_server_room,
            sql_location_idu, sql_earthing, sql_distance_idu, sql_security, sql_ducts, sql_unit, sql_install_type,
            sql_isp, sql_radio_type, sql_isp_type, sql_isp_name, sql_snr_rx, sql_bs, sql_remark);

And when i want to retrieve, its also in string format: 而且当我想检索时,它也是字符串格式:

public String getInfoData() {
    String[] columns = new String[]{ROW_ID, SYSAID_ID, SITE_ID, LINK_ID, CUSTOMER_NAME, SITE_CONTACT, TASK_TYPE, ADDRESS,
            REGION, PHONE, FAX, MOBILE, EMAIL, LANDLORD_NAME, RENT_STATUS, LOCATION, POWER_STATUS, VOLTAGE_MEASUREMENT, AIRCON,
            SERVER_ROOM_STATUS, LOCATION_IDU, EARTHING, DISTANCE_IDU, SECURITY_CABLES, DUCTS, UNIT_TYPE, INST_TYPE,
            HAVE_ISP, RADIO, ISP_TYPE, ISP_NAME, SNR_RX, BS, REMARKS};

How do i fix images data in these 如何修复这些图像数据

This will slow your database and application. 这会降低数据库和应用程序的速度。 Instead of this you can try to save to SDCard and keep URI at the db. 替代此方法,您可以尝试保存到SDCard并将URI保留在数据库中。

Very easy Two options 很简单两种选择

one:  
byte[] bytes = {...}  
String str = new String(bytes, "UTF-8");

or

public static String encodeTobase64(Bitmap image)
{
    Bitmap immagex=image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

    Log.e("LOOK", imageEncoded);  
    return imageEncoded;
}
public static Bitmap decodeBase64(String input) 
{
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0,         decodedByte.length); 
}

two: You can use GSON and make an object from all your text and ByteArray and what ever you want and than just do: GSON.toJson(); 第二:您可以使用GSON并根据您所有的文本和ByteArray以及您想要的内容创建一个对象,而不仅仅是这样做:GSON.toJson();

In a table , you can have many columns . 在一个表中,您可以有许多列。 To have text and images in same table you can have different columns for them with following data types :- 要在同一表中包含文本和图像,可以使用以下数据类型为它们提供不同的列:-

Text - VARCHAR 文字-VARCHAR

Images - BLOB 图片-BLOB

Sqlite is having a data type BLOB which can hold objects . Sqlite的数据类型为BLOB,可以容纳对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM