从相机或画廊节省sqlite的图像 [英] Saving an image on sqlite from camera or gallery

查看:180
本文介绍了从相机或画廊节省sqlite的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对SQLite的保存图像挣扎。
我想图像保存为在SQLite的BLOB。
我不知道如何声明的ImageView 字节[] ,因为我使用的插入方法对 dbAdapter
它是将图像保存到数据库的好办法吗?有些人说,文件保存路径URL到数据库中比较好。如果是好,请给我一些你的手。
我真是越来越艰难了点。
非常感谢。

 保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
    super.onActivityResult(要求code,结果code,数据);
     如果(结果code = RESULT_OK!)回报;
       开关(要求code)
       {
        案例PICK_FROM_CAMERA:
        捆绑额外= data.getExtras();
        ByteArrayOutputStream BOS =新ByteArrayOutputStream();
        位图selectedImage =(位图)extras.get(数据);
        selectedImage = Bitmap.createScaledBitmap(selectedImage,200,250,假);
        selectedImage.com preSS(比较pressFormat.PNG,0,BOS);
        mImageView.setImageBitmap(selectedImage);
        打破;        案例PICK_FROM_GALLERY:
            乌里selectedImageUri = data.getData();
            selectedImagePath =的getPath(selectedImageUri);
            的System.out.println(映像路径:+ selectedImagePath);
            mImageView.setImageURI(selectedImageUri);
        打破;
       }
}保护无效的onSaveInstanceState(捆绑outState)
{
    super.onSaveInstanceState(outState);
    saveState和();
}私人无效saveState和()
{
    字符串名称=(字符串)nameEdit.getText()的toString()。
    字符串类型=(字符串)categoryEdit.getText()的toString()。
    字符串EXPIRED_DATE =(字符串)expired_Date_Btn.getText()的toString()。
    //字节[] =图像(字节[])mImageView ......我要在这里做一些编码..
    如果(mRowId == NULL)
    {
        长ID = mDbHelper.insertItem(类别,名称,EXPIRED_DATE,图像);        如果(ID大于0)
        {
            mRowId = ID;
        }
    }
    其他
    {
        mDbHelper.updateItem(mRowId,类别,名称,EXPIRED_DATE,图像);
    }
}

DbAdapter类

 众长insertItem(字符串类,字符串名称,字符串EXPIRED_DATE,字节[]图片)
{
    ContentValues​​ initialValues​​ =新ContentValues​​();
    initialValues​​.put(KEY_CATEGORY,类别);
    initialValues​​.put(KEY_NAME,名);
    initialValues​​.put(KEY_EXPIRED_DATE,EXPIRED_DATE);
    initialValues​​.put(KEY_IMAGE,图像);
    返回db.insert(DATABASE_TABLE,空,initialValues​​);
}


解决方案

试着做这样的:

  ConvertDrawableToByteArray(mImageView.getDrawable()

通过ConvertDrawabelToByteArray这样定义的:

 公共静态的byte [] ConvertDrawableToByteArray(可绘制drawableResource){    位图imageBitmap =((BitmapDrawable)drawableResource).getBitmap();
    ByteArrayOutputStream imageByteStream =新ByteArrayOutputStream();
    imageBitmap.com preSS(Bitmap.Com pressFormat.PNG,100,imageByteStream);
    字节[] = imageByteData imageByteStream.toByteArray();
    返回imageByteData;
}

虽然我认为你应该能够得到字节出位图的为好。所有这一切说,存储SQL精简版的文件路径可能会更有效,但是,如果用户删除图像,你不得不处理,在您的应用程序。

I am struggling with saving image on sqlite. I am trying to save image as blob on sqlite. I don't know how to declare imageView to byte[] because I am using insert method on dbAdapter. Is it good way to save the image into database? some people said that saving file path url into database is better. If it is better, please give me some your hands. I am really getting tough for that. Many thanks.

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode != RESULT_OK) return;
       switch (requestCode)
       {
        case PICK_FROM_CAMERA:
        Bundle extras = data.getExtras();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Bitmap selectedImage = (Bitmap) extras.get("data");
        selectedImage = Bitmap.createScaledBitmap(selectedImage, 200, 250, false);
        selectedImage.compress(CompressFormat.PNG, 0, bos);
        mImageView.setImageBitmap(selectedImage);
        break;

        case PICK_FROM_GALLERY:
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            mImageView.setImageURI(selectedImageUri);
        break;
       }
}

protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    saveState();
}

private void saveState() 
{
    String name = (String) nameEdit.getText().toString();
    String category = (String) categoryEdit.getText().toString();
    String expired_date = (String) expired_Date_Btn.getText().toString();
    //byte[] image = (byte[]) mImageView... I HAVE TO DO SOME CODING HERE.. 
    if(mRowId == null)
    {
        long id = mDbHelper.insertItem(category, name, expired_date, image);

        if(id>0)
        {
            mRowId = id;
        }           
    }
    else 
    {
        mDbHelper.updateItem(mRowId, category, name, expired_date, image);
    }   
}

DbAdapter class

    public long insertItem(String category, String name, String expired_date, byte[] image)
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_CATEGORY, category);
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_EXPIRED_DATE, expired_date);
    initialValues.put(KEY_IMAGE, image);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

解决方案

Try doing this:

ConvertDrawableToByteArray(mImageView.getDrawable()

With ConvertDrawabelToByteArray defined like this:

public static byte[] ConvertDrawableToByteArray(Drawable drawableResource) {

    Bitmap imageBitmap = ((BitmapDrawable) drawableResource).getBitmap();
    ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
    imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, imageByteStream);
    byte[] imageByteData = imageByteStream.toByteArray();
    return imageByteData;
}

Though I think you should be able to get the bytes out of the Bitmap as well. All that said, storing the filepath in sql lite would probably be more efficient, however, if the user deletes the image you'd have to handle that in your application.

这篇关于从相机或画廊节省sqlite的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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