how to save image in sqllite database?

后端 未结 3 888
囚心锁ツ
囚心锁ツ 2021-01-28 05:11

this is my code below which browse imagew gellery and pick image but how do insert image view in database? this code sucessfuly show on screen selected image but not store in da

相关标签:
3条回答
  • 2021-01-28 05:38

    Please use below code for store images into sqlite database, it will solve your problem.

    MySQLActivity.java:-

    public class MySQLActivity extends Activity implements OnClickListener
    {
        protected static TextView textView;
        protected static ImageView bmImage;
        protected Button start;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            bmImage = (ImageView)findViewById(R.id.imageView1);
            textView = (TextView) findViewById(R.id.textView1);
    
            start = (Button) findViewById(R.id.button1);
            start.setOnClickListener(this); 
    
            DownloadFile();
        }
    
        public void onClick(View v) 
        {
            SQLiteDatabase myDb;       
            String MySQL;
            int icount;
            byte[] byteImage1 = null;
            byte[] byteImage2 = null;
            MySQL="create table emp1(_id INTEGER primary key autoincrement, fio TEXT not null, picture BLOB);";
            myDb = openOrCreateDatabase("/sdcard/Nick/MyWeatherDB.db", Context.MODE_PRIVATE, null);
    
            String s=myDb.getPath();
            textView.append("\r\n" + s+"\r\n");       
            myDb.execSQL("delete from emp1");
            ContentValues newValues = new ContentValues();
            newValues.put("fio", "Иванов Петр Сергеевич");
    
            /////////// insert picture to blob field ///////////////////// 
            try
            {
                FileInputStream instream = new FileInputStream("/sdcard/Dipak/Keshariya.png"); 
                BufferedInputStream bif = new BufferedInputStream(instream); 
                byteImage1 = new byte[bif.available()]; 
                bif.read(byteImage1); 
                textView.append("\r\n" + byteImage1.length+"\r\n"); 
                newValues.put("picture", byteImage1); 
    
                long ret = myDb.insert("emp1", null, newValues); 
                if(ret<0) textView.append("\r\n!!! Error add blob filed!!!\r\n");
            } catch (IOException e) {
                textView.append("\r\n!!! Error: " + e+"!!!\r\n");   
            }
    
            ////////////Read data ////////////////////////////  
            Cursor cur = myDb.query("emp1",null, null, null, null, null, null);
            cur.moveToFirst();
            while (cur.isAfterLast() == false)
            {
                textView.append("\r\n" + cur.getString(1)+"\r\n");
                cur.moveToNext();
            }
    
            ///////Read data from blob field////////////////////
            cur.moveToFirst();
            byteImage2=cur.getBlob(cur.getColumnIndex("picture"));
            bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0, byteImage2.length));
            textView.append("\r\n" + byteImage2.length+"\r\n"); 
    
            //////////////////////////    
            cur.close();
            myDb.close();
      }
    
      public void DownloadFile() 
      {   
          Bitmap bitmap1 = null;                    
          bitmap1 = BitmapFactory.decodeFile("/sdcard/Dipak/Dipak.jpg"); //weather.png");
          bmImage.setImageBitmap(bitmap1);
      }
    }
    
    0 讨论(0)
  • 2021-01-28 05:43

    i would suggest to avoid saving images to database , not only on android .

    this makes sense since it will slow down anything that is related to the database . also , the cursor won't be able to cache the results in order to move between the rows of the tables.

    instead , put a filename to point to a path on your app's storage . you could also add where it's stored (internal/external storage) .

    0 讨论(0)
  • 2021-01-28 05:57

    First make field in sqlite as blob datatype

    than get byte and store in databse image.getBytes();

    or if you have bitmap than convert into byte array

    and store into database

     public static byte[] getBitmapAsByteArray(Bitmap bitmap, boolean type) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        if (type) {
            bitmap.compress(CompressFormat.PNG, 0, outputStream);
        } else {
            bitmap.compress(CompressFormat.JPEG, 0, outputStream);
        }
        return outputStream.toByteArray();
    }
    
    0 讨论(0)
提交回复
热议问题