Save image URI in sqllite databsae - android

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

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

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!

Related

How to pick those photo stored in cloud from Google Photos?

I know how to pick photo from gallery and it always work.
The code to pick photo from gallery:
Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQ_GALLERY);
And code to handle result in onActivityResult():
Uri uri = data.getData();
Cursor cursor = getContext().getContentResolver().query(uri, new String[] {
MediaStore.Images.Media.DATA,
}, null, null, null);
if (cursor == null) return false;
if (cursor.moveToFirst()) {
String path = cursor.getString(0);
if (path != null) {
startPhotoEdit(new File(path), output, requestCode);
cursor.close();
return true;
}
}
cursor.close();
This time I pick them from Google Photos and the photo is still stored in cloud and not downloaded to local yet. After I pick one and it starts downloading like that:
photo_downloading
When the download task finished and return to my application, the _data column in cursor which always contains photo path is null.
Can someone help me, please.
I finally find that when photo from cloud is picked, its Uri is different. Like this:
content://com.google.android.apps.photos.contentprovider/1/1/mediakey%3A%2Flocal%253A131b7978-cc48-4206-8561-d18f99421551/ORIGINAL/NONE/2134743478
It contains mediakey instead of content in normal Uri.
So, this might be helpful:
getContentResolver().openInputStream(uri);

Missing EXIF info in images selected from gallery using EXTERNAL_CONTENT_URI

I'm trying to gather EXIF info (date of picture taken, geotagging, orientation) from images selected from an EXTERNAL_CONTENT_URI intent, but it seems that if the pictures come from the internet (e.g. Google Photos) the EXIF data is somehow truncated.
For example if I download a picture from a web browser from photos.google.com on my PC, its size is 4.377.104 bytes, and all the EXIF data are there.
While if I download the same exact image using the command:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
its size is 4.363.578 bytes (13526 bytes less than the original) and all the EXIF data are lost
Any idea on how to download the full original image?
PS: if I select a picture from the gallery which was taken from the phone and it is still resident on the phone's storage, the EXIF data are present
I finally found out a solution. after getting the source image Uri in the onActivityResult method using
Uri selectedImage = data.getData();
I use the following function to get the necessary data from the picture
public static ImageInfo getImageInfo(Context context, Uri photoUri) {
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] {
MediaStore.Images.ImageColumns.ORIENTATION,
MediaStore.Images.ImageColumns.LATITUDE,
MediaStore.Images.ImageColumns.LONGITUDE,
MediaStore.Images.ImageColumns.DATE_TAKEN } , null, null, null);
if (cursor.getCount() != 1) {
return null;
}
cursor.moveToFirst();
ImageInfo i = new ImageInfo();
i.Orientation = cursor.getInt(0);
i.Lat = cursor.getDouble(1);
i.Lon = cursor.getDouble(2);
i.DateTakenUTC = cursor.getLong(3)/1000;
cursor.close();
return i;
}

ImageView not populating from file path

I can't get my ImageViews to update from either URI or file path - they just don't show an image
Intent to capture image:
Intent photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photo, 1);
On ActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Uri imageUri = data.getData();
filePath = getRealPathFromURI(this, imageUri);
}
GetRealPathFromURI class:
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
It then inserts 'filePath' from onActivityResult in to a db
Retrieving from db and updating ImageViews
imgfilepath[y] = cursorc.getString(cursorc.getColumnIndex("IMAGE"));
imgFile[y] = new File(imgfilepath[y]);
Uri uri = Uri.fromFile(imgFile[y]);
String path = uri.getPath();
mImage.setImageURI(uri);
I've tried so many different ways to setImageBitmap etc which haven't worked (I can't remember all of them) - Can anyone see why this is not showing the image?
The image is in emulated storage and not the SD card.
EDIT:
I've added EXTRA_OUTPUT tag but I can't see the image in DDMS anywhere & the camera does not exit after taking the picture/accepting the image
Intent photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
root.mkdirs();
final String fname = "img_"+ System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
mImageUri = Uri.fromFile(sdImageMainDirectory);
photo.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(photo, 1);
Uri imageUri = data.getData();
ACTION_IMAGE_CAPTURE does not return a Uri.
The image is in emulated storage
Perhaps that one camera app does, in which case that camera app has a bug, to go along with the return-a-Uri bug.
There are thousands of Android device models. These ship with hundreds of different camera apps pre-installed, and there are hundreds more available from the Play Store and elsewhere. Many will have ACTION_IMAGE_CAPTURE implementations. Most should follow the documented protocol. None should save the image for your request, because you did not tell the camera app where to save the image.
Either:
Provide a location, via EXTRA_OUTPUT, for the camera app to save the image to, then load the image from that location, or
Use data.getExtra("data") to get a Bitmap that represents a thumbnail-sized image, if you do not provide EXTRA_OUTPUT

Get Path and Filename from Camera intent result

I want to make a picture with the camera intent and save it to the default DCIM folder. Then I want to get the path/filename where the picture is stored.
I am trying it with the following code:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE);
With this code, the camera opens and after I have taken one picture it closes and saves the picture to the default image folder (usually /dcim/camera or sdcard/dcim/camera...)
but how can I get the path and filename of the taken picture now?
I have tried almost everything in onActivityResult
I tried
String result = data.getData();
and
String result = data.getDataString();
and
String result = data.toURI();
and
Uri uri = data.getData();
etc.
I researched the last two days to find a solution for this, there are many articles on the web and on stackoverflow but nothing works.
I don't want a thumbnail, I only want the path (uri?) to the image that the camera has taken.
Thank you for any help
EDIT:
When I try:
path=Environment.DIRECTORY_DCIM + "/test.jpg";
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
it does not store the image as test.jpg but with the normal image name 2011-10-03.....jpg (but that is ok too, i only need the path to the image, it does not matter what the name is).
Best regards
EDIT Again
path=Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
File file = new File(path,"test111111111.jpg");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
When I try this, it stores the image to the right folder and with the given name (e.g. test111111.jpg).
But how can I get the filepath now in onActivityResult?
The picture will be stored twice, first on gallery folder, and after on the file you especified on putExtra(MediaStore.EXTRA_OUTPUT, path) method.
You can obtain the last picture taken doing that:
/**
* Gets the last image id from the media store
* #return
*/
private int getLastImageId(){
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
if(imageCursor.moveToFirst()){
int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
Log.d(TAG, "getLastImageId::id " + id);
Log.d(TAG, "getLastImageId::path " + fullPath);
imageCursor.close();
return id;
}else{
return 0;
}
}
This sample was based on post: Deleting a gallery image after camera intent photo taken
you can use like this in onActivityResult()
if(requestCode==CAMERA_CAPTURE)
{
Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
if(cursor != null && cursor.moveToFirst())
{
do
{
String picturePath =cursor.getString(cursor.getColumnIndex(Media.DATA));
Uri selectedImage = Uri.parse(picturePath);
}
while(cursor.moveToNext());
cursor.close();
File out = new File(picturePath);
try
{
mOriginal = decodeFile(out);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
mSelected.setImageBitmap(mOriginal);
}
}
When you are starting the ACTION_IMAGE_CAPTURE you can pass an extra MediaStore.EXTRA_OUTPUT as the URI of the file where you want to save the picture.
Here is a simple example:
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
EDIT: I just tried on my device and file.createNewFile() solved the problem for me.

Getting full-sized image from the camera does not work on Galaxy S

I am having a problem capturing an image from the built-in camera app on the Samsung Galaxy S.
I have a button on my app that when pressed launches the camera:
ContentValues values = new ContentValues();
values.put(Images.Media.MIME_TYPE, "image/jpeg");
mPicUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPicUri.getPath());
startActivityForResult(intent, REQ_CODE_TAKE_PIC);
From what I read on the interwebz, I can get the full-sized just-taken picture using the URI I passed to the intent. And so I have this on my onActivityResult:
Uri selectedImage = mPicUri;
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
And then using the variable filePath, I set the image in an ImageView. I am getting no image, and when I stepped through the code, I found out that BitmapFactory.decodeFile() is returning null.
Bitmap chosenPic = BitmapFactory.decodeFile(filePath);
So I tried a little more debugging. I found out that mPicUri returns a seemingly valid URI, such as: content://media/external/images/media/90. After the picture is taken and the user chooses to save the picture, the cursor resolves to the following filePath: /sdcard/DCIM/Camera/1285601413961.jpg. No bitmap is decoded though, BUT when I looked through the gallery, the picture I just took is there. So I tried to take a look at the URI of that picture, and this is what I got:
URI is: content://media/external/images/media/91
File path is: /sdcard/DCIM/Camera/2010-09-27 23.30.30.jpg
And so, it looks like the value I got from mPicUri is not the final URI that will be used.
Am I missing a step here? All I really want to do is retrieve the file of the just-taken picture.
Thanks in advance,
Zarah.
Did you try to specify File in both cases before taking picture and the same after?
public static File getTempFile() {
//it will return /sdcard/image.jpg
final File path = new File(Environment.getExternalStorageDirectory(), "MyApp");
if (!path.exists()) {
path.mkdir();
}
return new File(path, "image.jpg");
}
I'm using this function to get and set file path.

Categories

Resources