将图像存储在SQLite数据库中

时间:2018-02-12 14:59:27

标签: android sqlite android-sqlite

这是我的Doctor.java文件。该应用程序是这样的,当用户通过列表视图来到此页面时,他将自动查看医生列表。现在我已经和下面的医生一起填充了数据库,但是如果我也可以把医生的图像放在那里真的很好。我已将每位医生的图像存储在drawable文件夹中,但我不知道如何将它们放入数据库中。有谁可以帮助我吗。 Doctor.java

package net.limb.healthservices;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

/**
 * Created by purus on 03-Feb-18.
 */

public class Doctor extends Activity implements View.OnClickListener {
SQLiteDatabase db;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_doctor);
    TextView textView= (TextView) findViewById(R.id.textView5);
    String doc= getIntent().getStringExtra("type");
    String authentication= getIntent().getStringExtra("auth");
    textView.setText(doc);
    db=openOrCreateDatabase("DoctorDB", Context.MODE_PRIVATE,null);
    db.execSQL("CREATE TABLE IF NOT EXISTS doctor(Name VARCHAR,degree VARCHAR,address VARCHAR,phnumber VARCHAR,dept VARCHAR);");
    db.execSQL("INSERT INTO doctor VALUES('Sujoy Khan','MBBS,MRCP,DRCPATH,FRCPATH,FRCP','58, Canal Circular Road, Kadapara, Apollo Gleneagles Hospital Limited, Kolkata,West Bengal 700054.','18605001066','Allergist');");
    db.execSQL("INSERT INTO doctor VALUES('Allergy & Asthma Treatment Centre',' ','155A, A. J. C. Bose Road, Acharya Jagadish Chandra Bose Rd, Entally, Kolkata, West Bengal 700014','9874555339','Allergist');");
    db.execSQL("INSERT INTO doctor VALUES('Amitava Chakraborty','MBBS,MRCP,DRCPATH,FRCPATH,FRCP','207, Netaji Subhash Chandra Bose Rd, Bansdroni, Kolkata, West Bengal 700040.','03324815741','Allergist');");
    db.execSQL("INSERT INTO doctor VALUES('Ranjan Shrivatava','MBBS, Diploma in Cardiology, MD - Medicine','Nazrul Islam Avenue, VIP Road, Landmark: Opposite Haldiram, Kolkata','+91 33794 14541','Cardiologist');");
    db.execSQL("INSERT INTO doctor VALUES('Arindam Pande','MBBS, MD - General Medicine, DM - Cardiology, FACC, FESC','59, Kalicharan Ghosh Road, Sinthee, Landmark: Near Sinthee Police Station, Kolkata','+913379415380','Cardiologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Ranjan Kumar Sharma','MBBS, DNB (Cardiology), MD - General Medicine, DM - Cardiology, DIP Card','73 & 81B , Bagmari Road,, Landmark: Near Ultadanga Hudco Stop, Kolkata','+913379415380','Cardiologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Gautam Banerjee','MBBS,MRCP,DRCPATH,FRCPATH,FRCP','58, Canal Circular Road, Kadapara, Apollo Gleneagles Hospital Limited, Kolkata,West Bengal 700054.','033 2555 6244','Dermatologist');");
    db.execSQL("INSERT INTO doctor VALUES('Sabyasachi Mazumdar','MD - Dermatology, MBBS','FA-53, Ramakrishna Nagar, Laskarpur, Garia BT. Landmark: Kavi Nazrul Metro    Station., Kolkata','+91 33794 15380','Dermatologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr Sayantani Chakraborty','MBBS, MD - Dermatology','DA 8, Sector 1, Landmark: Near Katerpillar Cafe, Kolkata','+91 33794 14551','Dermatologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Supratik Bhattacharyya','MBBS, MD - Medicine, MRCP (UK), FACP (USA), MS - Endocrinology & Diabetes','1st floor, Street Number 124, BG Block, Action Area B, Landmark: Near Rajarhat,  Kolkata','+91 33794 14411','Endocrinologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Kaushik Pandit','MBBS, MD - General Medicine, DNB - General Medicine, DM - Endocrinology','2/7, 1st Floor, Vasundara Building, Minto Park, Landmark: Beside Central Plaza    Building, Kolkata','+91 33794 14541','Endocrinologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Rachna Mazumder','MBBS, MD - General Medicine, DM - Endocrinology, MRCP (UK), FRCP - Internal    Medicine','30/B Prince Anwar Shah Road., Landmark: Opposite To ICICI Bank., Kolkata','+91 33794 14207','Endocrinologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Sanjoy Mandal','MBBS, MS - General Surgery, DNB - General Surgery, MNAMS - General Surgery','173, CB Block, Sector-1, Landmark: Opposite CB Block Community Center & Near    Tank Number 2, Kolkata','+91 33794 14551','Gastroenterologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Mahesh Kumar Goenka','MBBS, MD - Medicine, DM - Gastroenterology, FACG, FASGE','58, Canal Circular Lane, Kadapara, Kankurgachi, Landmark: Near Maniskar Mall,    Kolkata','+91 33794 15093','Gastroenterologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Vijay Kumar Rai','DM - Gastroenterology, MD - Internal Medicine, MBBS','P122, 1st Floor, CIT Road, Landmark: Next To Goli Vadapaav & Near Pantaloons,    Kolkata','+91 33794 14207','Gastroenterologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Avijit Das','DM - Neurology, Post Doctoral Fellowship in Neurological Rehabilitation, MD -   General Medicine, MBBS','230, Barakhola Lane, Purba Jadavpur, Landmark: Behind Metro Cash & Carry,  Kolkata','+91 33794 14258','Neurologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. S K Somani','MBBS, Diploma in Tropical Medicine Health, MD - General Medicine, DM -  Neurology','21, Prannath Pandit Street, Bhawanipur, Kolkata','+91 33794 14541','Neurologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Abhijit Chatterjee','MBBS,MD,DM (NEURO)','60, Motilal Nehru Road, Lansdowne, Kolkata, West Bengal 700029','033 2475 0334','Neurologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Pallavi Daga','MBBS, MS - Obstetrics & Gynaecology, MRCOG','78, Sambhunath Pandit Street, Landmark: Behind Gol Mandir and Opposite to    Rabindra Sadan Metro Station, Kolkata','+91 33794 15286 ','OBGYN');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Subidita Chatterjee','MBBS, DGO, DNB - Obstetrics & Gynecology','CF-391, Sector 1, Landmark: Near Purpo Bhavan., Kolkata','+91 33794 14551','OBGYN');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Sharmishtha Patra','MBBS, MS - Obstetrics & Gynaecology, MRCOG, FICRS','BH - 36, Sector - 2, Near Kathgola Stop, Salt Lake, Landmark: Opposite Bank of   Baroda., Kolkata','+91 33794 15093','OBGYN');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Tamohan Chaudhuri','MBBS, MD - Oncology, Diploma in Radio Therapy','House No. 7/14,Purbachal Complex,Sector-3, Kolkata','+91 33794 14551','Oncologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Prasenjit Chatterjee','MBBS, Diploma in Radio Therapy, DNB - Radiation Oncology, MD Radiation    Oncology','8/5, Alipore Road, Landmark: Near Natural Library, Alipore Zoo & Visa House,     Kolkata','+91 33794 15093','Oncologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Indranil Ghosh','DM - Oncology, MD - Pediatrics, MBBS','58, Canal Circular Road, Phulbagan, Kolkata','+91 33794 14551','Oncologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Siddhartha Ghosh','MBBS, MS - Ophthalmology','48/1F, Leela Roy Sarani, Ballygunge, Landmark: Near Dover Terrace, Kolkata','+91 33794 15093','Opthalmologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Pradeep Kumar Saraf','MBBS, MS - Ophthalmology, Fellowship in Vitreoretinal Surgery','51/1A, Near Motor Vehicles & Lansdown Padopukur, Sarat Bose Road. Landmark: Opp. to Health Point Nursing Home, Kolkata','+91 33794 14541','Opthalmologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Somnath Ghosh','Post Graduate Diploma in Ophthalmology, MBBS','P 40/A, Block D., Landmark: Near Bangur Boys School, Kolkata','+91 33794 15286','Opthalmologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Abhijit Sarkar','MBBS, MRCPCH (UK), Member of the Royal College of Paediatrics and Child Health (MRCPCH, London, Glasgow or Edinburgh), Diploma in Child Health (DCH), Diploma in Child Health (DCH)','AD-145, Sector-1, Landmark: Near Tank 4, Kolkata','+91 33794 14322','Pediatrician');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Sumita Saha','MBBS, Diploma in Child Health (DCH - UK), MRCPCH (UK)','730, Eastern Metropolitan Bypass, Anandapur, Landmark: Near Kolkata    International School & Near Ruby Hospital, Kolkata','+91 33794 15093 ','Pediatrician');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Ajit Kumar Verma','MBBS, Diploma in Child Health (DCH)','Shop #F20, NBCC Shoping Center, Landmark: Near New Town Bus Stand., Landmark:   New Town Bus Depot, Kolkata','+91 33794 14258','Pediatrician');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Indranil Saha','MBBS, MD - Psychiatry, DPM (Psychiatry)','P77, Green View, Baishnab Ghata Patuli, Kolkata 700084, Landmark: Opposite BRWS (Outdoor) Hospital, Kolkata','+91 33794 15093','Psychologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Prathama Chaudhuri','MBBS, MD - Psychiatry, DNB - Psychiatry, Fellowship in Child and Adolescent     Psychiatry, Certificate in Hypnotherapy','GD-347, GD Block, Sector III, Kolkata, Landmark: Near G D Community Hall, Kolkata','+91 33794 14298','Psychologist');");
    db.execSQL("INSERT INTO doctor VALUES('Dr. Amitabha Mukerji','MBBS, MD - Psychiatry','256, Block-B, Landmark: Near Lake Town Nursing Home, Kolkata','+91 33794 14258','Psychologist');");

}

@Override
public void onClick(View v) {



}
}

这是我的doctor.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">


<ImageView
    android:id="@+id/imageView2"
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:layout_below="@+id/textView5"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dp"
    app:srcCompat="@drawable/docdefault" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:text=""
    android:textSize="22dp" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="22dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView6"
    android:layout_alignStart="@+id/textView6"
    android:layout_below="@+id/textView6"
    android:layout_marginTop="17dp"
    android:text="TextView" />

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView9"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="11dp"
    android:ems="10"
    android:inputType="textPostalAddress" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText"
    android:layout_alignStart="@+id/editText"
    android:layout_below="@+id/editText"
    android:layout_marginTop="17dp"
    android:ems="10"
    android:inputType="phone" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_alignStart="@+id/editText2"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="21dp"
    android:ems="10"
    android:inputType="time" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/editText3"
    android:layout_marginEnd="29dp"
    android:layout_marginRight="29dp"
    android:layout_marginTop="16dp"
    android:text="Button" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

有许多不同的方法可以在数据库中存储图像。 最流行的可能是将图像格式化为base64字符串并保存字符串,或将其转换为字节数组并将其存储为blob。

将可绘制图像转换为base64字符串的示例:

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {

    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    image.compress(compressFormat, quality, byteArrayOS);
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.NO_WRAP);
}


BitmapDrawable drawable = (BitmapDrawable) getDrawable(R.drawable.your_image);
Bitmap bitmap = drawable.getBitmap();
encodedImage = encodeToBase64(bitmap, Bitmap.CompressFormat.JPEG, 100); //Make sure you choose the correct CompressFormat for your image eg. PNG, JPG, ...