如何从内部存储中选择图像并将其插入 SQLite? [英] How to select an image from internal storage and insert it in SQLite?

查看:49
本文介绍了如何从内部存储中选择图像并将其插入 SQLite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击我的应用程序中的按钮时,他必须能够从内部存储(SD 卡或平板电脑/手机内存)中选择图像.然后,我希望将此图像存储在 SQLite 数据库中.

When the user clicks a button in my app, he has to be able to select an image from the internal storage (sd card or tablet/phone memory). Then, I want this image to be stored in a SQLite database.

所以有三个问题:

  • 如何向用户显示图库或文件浏览器,以便他可以导航和选择图像?
  • 应用如何知道选择了哪张图片?
  • 我的应用如何将图像存储在数据库中(BLOB 数据)?

推荐答案

好的,正如 Lingviston 已经指出的那样.您可以从此处.

Alright, as Lingviston already pointed out. You can implement picking an image from the gallery from here.

至于存储图片,我将稍微编辑一下链接中的代码.

As for storing the image, I'm going to edit the code in the link a little bit.

取而代之的是:

ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

我要将所选图像存储在 Bitmap 中.

I'm going to store the selected image in a Bitmap.

ImageView imageView = (ImageView) findViewById(R.id.imgView);
Bitmap mBitmap = BitmapFactory.decodeFile(picturePath);
        imageView.setImageBitmap(mBitmap);

现在要在android中存储为BLOB类型,您需要将位图转换为字节,然后将字节数组存储在sqlite数据库中.

Now to store in as a BLOB type in android you need to convert the bitmap into bytes and then store the byte array in the sqlite database.

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] imageInByte = stream.toByteArray();

现在你只需要传递imageInByte 来存储在SQLite 数据库中.

Now you just need to pass imageInByte to store in the SQLite database.

顺便说一句,这两个答案都已经在互联网上提供,并且 StackOverflow 单独提供,您只需要将 2 和 2 放在一起.在发布问题之前,请尝试彻底搜索.

As a side note, both of these answers were already available all over the internet and StackOverflow individually, you just had to put 2 and 2 together. Please try searching thoroughly before you post a question.

这篇关于如何从内部存储中选择图像并将其插入 SQLite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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