0

I am creating an Android app for class that will take you to the camera app if you click the "capture" button on my user interface. It then returns you to the app and displays the images thumbnail and if you click the "save" button, it will save the button in a SQLite database that I haven't created yet.

My problem is trying to figure out how to access the actual picture and its data, not information about the thumbnail.

The tutorial I was reading has information for storing the image in Android's built in Android's directories, but I can not figure out how to access the actual fullsize image data to store into the database I will be creating myself.

This is the link to the tutorial: https://developer.android.com/training/camera/photobasics#java

This is not the complete code, just a snipet and it was created in Android Studio:

'''
ImageView pic;

public void capture(View view) {
    dispatchTakePictureIntent();
}

public void save(View view) {
}

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        pic.setImageBitmap(imageBitmap); // sets the imageview on the UI as the thumbnail
    }
}

'''

VikaS GuttE
  • 1,636
  • 2
  • 14
  • 38
Mandy
  • 1
  • 1
  • Storing photos in a database is not a good pattern in Android app development. Save the photos to files (perhaps in [internal storage](https://commonsware.com/blog/2017/11/13/storage-situation-internal-storage.html)), and save a path to the photo in the database. – CommonsWare Mar 23 '19 at 17:18
  • My assignment is to store them in a SQLite database I have to create. – Mandy Mar 23 '19 at 17:21
  • Thanks for the info, though. – Mandy Mar 23 '19 at 17:31

1 Answers1

0

I believe that you need to pass a URI of a storage location (file), via the takePictureIntent intent using

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, your_image_Uri);

You can then open the file and retrieve it into a byte[] and store it as a blob. The SQLiteDatase convenience, insert method makes this very easy.

e.g. assuming the byte[] is name my_picture then

ContentValues cv = new ContentValues();
cv.put(column_name_for_picture,my_picture);
long id = insert("the_table_name",null,cv);

You retrieve the stored picture from the db using the SQLiteDatabase query method and then extract the byte[] using the Cursor's getBlob method.

However

If the picture, is 2MB or larger, you will not be able to retrieve it as there is a limitation of 2MB for a CursorWindow (1MB for older versions of Android). I'd suggest that even at 1MB you may well have issues retrieving the image(s).

The recommended way is to not store the image in the database, but to store the path of a file where the image is stored in the database.

You may wish to have a look at How to use images in Android SQLite that are larger than the limitations of a CursorWindow? as this shows a work-around albeit NOT recommended.

Community
  • 1
  • 1
MikeT
  • 51,415
  • 16
  • 49
  • 68