Snake
Snake

Reputation: 14648

Save image URI in sqllite databsae

Can I store the image URI in sql DB so that I can retrieve it later to show the image? Or does it aways has to be full absolute path?

Thank you

Upvotes: 0

Views: 112

Answers (2)

Hisham
Hisham

Reputation: 353

Yes you can.

For example if you pick/take image, you will start the picker/camera intent, and you will get the result in onActivityResult, here you should get the image uri.

If the user pick from the gallery, you should do the following to get the uri.

String imageUri = null;
Cursor cursor = mContext.getContentResolver().query(uri, new String[] {android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
cursor.moveToFirst();
if (cursor != null) {
    imageUri = cursor.getString(0);
}

cursor.close();

Now, save imageUri

If the user take image from the camera;

File photoFile = new File("your directory", "name");

Intent takePicIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePicIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePicIntent, ACTION_TAKE_PHOTO);

Then, in onActivityResult

String imageUri = photoFile.getPath();

And save it!

Upvotes: 0

tyczj
tyczj

Reputation: 73753

you sure can store it in the database, convert it to a string and convert it back when you want to use it

Upvotes: 1

Related Questions