0

i'm making an app where the user can choose images from the phone's album and store it into a table in sqlite database, i have seen many posts here about this issue but couldn't understand the solutions (neither did they work for me), in my activity i have an imageview which when clicked will open the album and allow the user to choose an image and also i have a button which when clicked will store the image in sqlite database, i'm just able to choose the image but after that i'm stuck, i got to this method in my code:

public void getImage(View view) throws IOException {
        ImageView v=(ImageView)view;
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent,1);
    }


    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContext().getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                image.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();

            }
        }
    }

now what should i do after this to store the image in the db?

M.KH
  • 388
  • 1
  • 5
  • 20
  • You probably don't want to store a blob in a relational db. – nylanderDev Aug 02 '20 at 20:27
  • i found a solution that says i could store the image as a file inside the app itself and store the path of that file in the db, but no explanatory code was provided though so i don't know about this – M.KH Aug 02 '20 at 20:30
  • 1
    Yeah, storing the path should be fine. – nylanderDev Aug 02 '20 at 20:35
  • could you please help directing me to a code tutorial of this? – M.KH Aug 02 '20 at 20:37
  • Check https://stackoverflow.com/questions/15662258/how-to-save-a-bitmap-on-internal-storage for how to store a bitmap to internal storage. The same question also addresses the issue of getting the file name. – Kasalwe Aug 02 '20 at 22:14

1 Answers1

1

If you are sure about the images will not be removed from internal storage, you can save the image path. If want to save the image data, you can do the following.

for selecting image

private void selectImage() {
     Intent intent = new Intent();
     intent.setType("image/*");
     intent.setAction(Intent.ACTION_GET_CONTENT);
     startActivityForResult(intent, IMAGE_REQ);
}

In onActivityResult, from Intent data ...

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMAGE_REQ && resultCode == Activity.RESULT_OK && data != null) {
            Uri path = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), path);
                Bitmap.createScaledBitmap(bitmap, 150, 150, true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

get byte[] from bitmap

public static byte[] getByteArrayFromBitmap(Bitmap bitmap) {
        if(bitmap == null) return null;

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);

        return byteArrayOutputStream.toByteArray();
    }

then save the byte[] as blob in sqlite

for getting the images as bitmap

public static Bitmap getBitmapFromByteArray(byte[] blob){
        if(blob == null) return null;

        Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
        return bitmap;
    }
Eishon
  • 1,274
  • 1
  • 9
  • 17
  • hey thanks for the reply! but why do you think that images won't be removed from the internal storage? if i wanted to remove an image from my app i believe i can use the deletion method mentioned here, right? https://stackoverflow.com/questions/3554722/how-to-delete-internal-storage-file-in-android – M.KH Aug 03 '20 at 11:20
  • 1
    If you store the images in database, you need to execute SQL statements for modifications. If you store the images as files, you can use the refference you mentioned. – Eishon Aug 03 '20 at 17:27