info
info

Reputation: 2182

Getting an image from SD card and store it in Android Database

Can i get an image from SD card and store it in the application database?? Is this idea effective in my application??

Thanks in advance!!

Regards

Upvotes: 0

Views: 1366

Answers (1)

Prasham
Prasham

Reputation: 6686

Yes you can get the image from your sd card and store it in sqlite database, we have done it but it will be too expensive for your device....... So be careful about that,

call image picker intent by calling this

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                    startActivityForResult(i, 12);

It will open default gallery picker view , when you select an image you will be returned in your current activity, So override onActivityResult() and look for result code 12.

in that code you can get cursor by these lines

Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
cursor.moveToFirst();

From that cursor object you can get the file path of image by these lines

 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
 String filePath = cursor.getString(columnIndex);

Here is the bitmap object

Bitmap bMap = BitmapFactory.decodeFile(filePath);

Get byte arrays

ByteArrayOutputStream out = new ByteArrayOutputStream();

bMap.compress(Bitmap.CompressFormat.PNG, 100,out); 
byte[] imageArray = out.toByteArray();

Now you have to store imageArray as blob in your database. And you are done.... Don't forget to refer this post from where i got the solution...

Upvotes: 1

Related Questions