如何保存图像sqllite数据库? [英] how to save image in sqllite database?

查看:163
本文介绍了如何保存图像sqllite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code,低于该浏览imagew gellery,并挑选形象,如何插入图像显示在数据库中?这code sucessfuly在屏幕上显示所选图像而不是存储在数据库中如何通过图片参数?在该行的地方我评论/的我写的东西在这里 /

 进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.Intent;
进口android.database.Cursor;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.net.Uri;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.provider.MediaStore;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.ImageView;
进口android.widget.Toast;

公共类AddEditCountry延伸活动{

 专用长ROWID;
 私人的EditText nameEt;
 私人的EditText卡佩;
 私人的EditText codeET;
 私人的EditText Donedate;
 私人的EditText注意事项;
 私人的EditText人;
 私人ImageView的imageView1;
 位图yourSelectedImage;
 @覆盖
 公共无效的onCreate(包savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.add_country);

    nameEt =(EditText上)findViewById(R.id.Address);
    卡佩=(EditText上)findViewById(R.id.Stage);
    codeET =(EditText上)findViewById(R.id.Dueby);

    Donedate =(EditText上)findViewById(R.id.Donedate);

    注=(EditText上)findViewById(R.id.Notes);
    人=(EditText上)findViewById(R.id.Person);

    imageView1 =(ImageView的)findViewById(R.id.imageView1);
    按钮浏览=(按钮)findViewById(R.id.Browse);

    Browse.setOnClickListener(新View.OnClickListener()
    {
        公共无效的onClick(视图v)
        {
                意向意图=新的意图(Intent.ACTION_GET_CONTENT);
                intent.setType(图像/ *);
                startActivityForResult(意向,0);
        }
    });

    捆绑额外= getIntent()getExtras()。

    如果(临时演员!= NULL)
    {
         ROWID = extras.getLong(ROW_ID);
         nameEt.setText(extras.getString(名字));
         capEt.setText(extras.getString(上限));
         codeEt.setText(extras.getString(code));
         Donedate.setText(extras.getString(位置));
         Notes.setText(extras.getString(票据));
         Person.setText(extras.getString(人));
         imageView1.setImageURI(yourSelectedImage);

    }

    按钮saveButton =(按钮)findViewById(R.id.saveBtn);
    saveButton.setOnClickListener(新OnClickListener(){

        公共无效的onClick(视图v)
        {
           如果(nameEt.getText()。长度()!= 0)
           {
              AsyncTask的<对象,对象,对象> saveContactTask =
                 新的AsyncTask<对象,对象,对象>()
                 {
                    @覆盖
                    保护对象doInBackground(对象... PARAMS)
                    {
                       saveContact();
                       返回null;
                    }

                    @覆盖
                    保护无效onPostExecute(对象结果)
                    {
                       完();
                    }
                 };

              saveContactTask.execute((对象[])NULL);
           }

           其他
           {
              AlertDialog.Builder警报=新
      AlertDialog.Builder(AddEditCountry.this);
              alert.setTitle(R.string.errorTitle);
              alert.setMessage(R.string.errorMessage);
              alert.setPositiveButton(R.string.errorButton,NULL);
              alert.show();
           }
        }
    });
   }

   私人无效saveContact()
   {
      DatabaseConnector dbConnector =新DatabaseConnector(本);

      如果(getIntent()。getExtras()== NULL)
      {
/ *我写的东西在这里进行图像* /
      dbConnector.insertContact(nameEt.getText()的toString()
                  capEt.getText()的toString()
                  codeEt.getText()的toString()
                  Donedate.getText()的toString()
                  Notes.getText()的toString()
                  Person.getText()的toString(),空




                  );
      }
      其他
/ *我WIRTE这里图像* / {
         dbConnector.updateContact(ROWID,
            nameEt.getText()的toString()
            capEt.getText()的toString()
            codeEt.getText()的toString()
            Donedate.getText()的toString()
         Notes.getText()的toString()
          Person.getText()的toString(),空


         );
      }
   }











   保护无效onActivityResult(INT申请code,INT结果code,意图imageReturnedIntent){
       super.onActivityResult(要求code,因此code,imageReturnedIntent);

       开关(要求code){
       情况下0:
           如果(结果code == RESULT_OK){
               乌里selectedImage = imageReturnedIntent.getData();
               的String [] filePathColumn = {MediaStore.Images.Media.DATA};

               光标光标= getContentResolver()查询(selectedImage,filePathColumn,NULL,NULL,NULL);
               cursor.moveToFirst();

               INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
               字符串文件路径= cursor.getString(参数:columnIndex); //文件所选图像的路径
               cursor.close();
                       //使用下面一行转换文件的路径为位图图像。
               yourSelectedImage = BitmapFactory.de codeFILE(文件路径);

                       //把BitmapImage的在你的ImageView
               imageView1.setImageBitmap(yourSelectedImage);
           }
       }
    }







}
 

解决方案

首先要在SQLite作为BLOB数据类型字段

比获得字节,并存储在DATABSE image.getBytes();

或者如果你有比位图转换成字节数组

和存储到数据库

 公共静态的byte [] getBitmapAsByteArray(位图位图,布尔类型){
    ByteArrayOutputStream的OutputStream =新ByteArrayOutputStream();
    如果(类型){
        bitmap.com preSS(比较pressFormat.PNG,0,OutputStream的);
    } 其他 {
        bitmap.com preSS(比较pressFormat.JPEG,0,OutputStream的);
    }
    返回outputStream.toByteArray();
}
 

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 database how to pass image parameter? in that line where i comment/what i write here/

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class AddEditCountry extends Activity {

 private long rowID; 
 private EditText nameEt;
 private EditText capEt;
 private EditText codeEt;    
 private EditText Donedate;
 private EditText Notes;
 private EditText Person;
 private  ImageView imageView1;
 Bitmap yourSelectedImage;
 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.add_country);

    nameEt = (EditText) findViewById(R.id.Address);
    capEt = (EditText) findViewById(R.id.Stage);
    codeEt = (EditText) findViewById(R.id.Dueby);

    Donedate = (EditText) findViewById(R.id.Donedate);

    Notes = (EditText) findViewById(R.id.Notes);
    Person = (EditText) findViewById(R.id.Person);

    imageView1 = (ImageView) findViewById(R.id.imageView1);
    Button Browse = (Button) findViewById(R.id.Browse);

    Browse.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {               
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, 0);
        }
    });        

    Bundle extras = getIntent().getExtras(); 

    if (extras != null)
    {
         rowID = extras.getLong("row_id");
         nameEt.setText(extras.getString("name"));  
         capEt.setText(extras.getString("cap"));  
         codeEt.setText(extras.getString("code"));  
         Donedate.setText(extras.getString("Location"));  
         Notes.setText(extras.getString("Notes")); 
         Person.setText(extras.getString("Person")); 
         imageView1.setImageURI(yourSelectedImage);

    }

    Button saveButton =(Button) findViewById(R.id.saveBtn);
    saveButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) 
        {
           if (nameEt.getText().length() != 0)
           {
              AsyncTask<Object, Object, Object> saveContactTask = 
                 new AsyncTask<Object, Object, Object>() 
                 {
                    @Override
                    protected Object doInBackground(Object... params) 
                    {
                       saveContact();
                       return null;
                    }

                    @Override
                    protected void onPostExecute(Object result) 
                    {
                       finish();
                    }
                 }; 

              saveContactTask.execute((Object[]) null); 
           }

           else
           {
              AlertDialog.Builder alert = new  
      AlertDialog.Builder(AddEditCountry.this);
              alert.setTitle(R.string.errorTitle); 
              alert.setMessage(R.string.errorMessage);
              alert.setPositiveButton(R.string.errorButton, null); 
              alert.show();
           }
        } 
    });
   }

   private void saveContact() 
   {
      DatabaseConnector dbConnector = new DatabaseConnector(this);

      if (getIntent().getExtras() == null)
      {
/* what i write here for  image*/          
      dbConnector.insertContact(nameEt.getText().toString(),
                  capEt.getText().toString(),
                  codeEt.getText().toString(),
                  Donedate.getText().toString(),
                  Notes.getText().toString(),
                  Person.getText().toString(), null




                  );
      }
      else
/* what i wirte here for image*/      {
         dbConnector.updateContact(rowID,
            nameEt.getText().toString(),
            capEt.getText().toString(), 
            codeEt.getText().toString(), 
            Donedate.getText().toString(),
         Notes.getText().toString(),
          Person.getText().toString(), null


         );
      }
   }











   protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
       super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

       switch(requestCode) {
       case 0:
           if(resultCode == RESULT_OK){
               Uri selectedImage = imageReturnedIntent.getData();
               String[] filePathColumn = {MediaStore.Images.Media.DATA};

               Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
               cursor.moveToFirst();

               int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
               String filePath = cursor.getString(columnIndex); // file path of selected image
               cursor.close();
                       //  Convert file path into bitmap image using below line.
               yourSelectedImage = BitmapFactory.decodeFile(filePath);

                       // put  bitmapimage in your imageview
               imageView1.setImageBitmap(yourSelectedImage);
           }
       }
    }







}

解决方案

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();
}

这篇关于如何保存图像sqllite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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