2

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.

So there is three questions:

  • How can I show the gallery or the file explorer to the user, so he can navigate and select an image?
  • How will the app know which image was selected?
  • How can my app store the image in a database (BLOB data)?
moictab
  • 959
  • 6
  • 27
  • http://viralpatel.net/blogs/pick-image-from-galary-android-app/ and also note that you can access mostly all the mesia data through MediaStore content provider. – Lingviston Jun 15 '13 at 19:31

1 Answers1

1

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.

Instead of this:

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

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

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

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

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.

Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69