I am not able to get GEO Exif infos from picture taken from camera.
This is the code, and the "oldExif" variable does have almost all values NULL (also GEO infos).
The "uri" variable is the uri of the image taken and created.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != AppCompatActivity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_IMAGE_CAPTURE) {
try {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
InputStream in = getContentResolver().openInputStream(uri);
ExifInterface oldExif = new ExifInterface(in);
// oldExif does have almost all values NULL
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
My android version is 5.0 and I already enabled the set of the camera about localization.
Indeed if I get a picture out of my app and show infos, I see geo data.
There is no Intent action that:
Causes a third-party camera app to offer the user to take a picture, and
Forces that camera app to add any particular EXIF tags
Your choices are:
Live without the EXIF tags
Add those tags yourself (e.g., find the user's location and put the geotags in the image), if they are missing
Related
I've written code in Android to capture an image and then use that image as follows:
private static Intent i;
final static int cons = 0;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public void takePicture()
{
i= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Open camera
startActivityForResult(i, cons);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Log.d("Testing if condition", "Test"); //This code does not execute
Bundle ext = data.getExtras();
Log.d("Upload image", ext.toString());
sendForUpload(data); // function to upload file to server
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
This code lets me take a photo and it saves to the SD card. However, I'm not sure if the file is sent to the sendForUpload function where I've handled getting the path and uploading the file. In fact nothing inside the if (resultCode == RESULT_OK) block works. How do I use the image captured from this activity?
Well, you have a few problems.
First, you are calling startActivityForResult(i, cons);. cons is 0. You are trying to compare 0 with CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE. Since CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is 100, that will not work.
Second, you are calling data.getExtras().toString() (split over two Java statements). I would expect this to return a value like android.os.Bundle#34334143. If that is what you want to upload, fine. I am guessing that you want to upload the image. Since you did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, you get a Bitmap of the thumbnail image taken by the camera via data.getParcelableExtra("data").
Third, you are making inappropriate assumptions:
This code lets me take a photo and it saves to the SD card.
There is no requirement for the camera app to save the image anywhere, let alone to removable storage. In fact, I would argue that this is a bug in your camera app. That is not surprising, as ACTION_IMAGE_CAPTURE implementations have bugs.
where I've handled getting the path and uploading the file
There is no path. You did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, and so all you get back is the thumbnail Bitmap.
I'm using this to put the image in a ImageView:
Uri uri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
You can use bitmap or uri objects for upload or show your picture..
Hope it helps you.
I am writing code to retrieve lat and long from image capture. I can able to take image using camera event and onActivityResult.
eprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri _uri = null;
Cursor cursor = null;
try {
final int PICK_IMAGE = 1;
if (requestCode == PICK_IMAGE && data != null
&& data.getData() != null) {
_uri = data.getData();
if (_uri != null) {
// User had pick an image.
cursor = getContentResolver()
.query(_uri,
new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
null, null, null);
cursor.moveToFirst();
// Link to the image
final String imageFilePath = cursor.getString(0);
// Toast.makeText(getApplicationContext(), imageFilePath,
// Toast.LENGTH_LONG).show();
imageLocation= imageFilePath;
File imgFile = new File(imageFilePath);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
captureImage.setImageBitmap(myBitmap);
}
cursor.close();
} else {
// Toast.makeText(getApplicationContext(), getSdCard,
// Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
} catch (Exception e) {
if (cursor == null || cursor.equals("")) {
String getSdCard = _uri.getPath();
imageLocation= getSdCard;
File imgFile = new File(getSdCard);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
captureImage.setImageBitmap(myBitmap);
}
}
e.printStackTrace();
}
}
From this how come we get the latitude and longitude from the image. i searched a while, i cant able to get the location.
What Monkeyless suggested is right, use the Exifinterface. There is a working example in the accepted answer to here:
How to get the latititude and longitude of an image in sdcard to my application?
The camera may or may not capture location data with the image, that depends on the user's camera app and whatever settings they are using (you can disable geo tagging photos, by default on most android phones it is disabled). If any location data is attached to the image, you can find it either using ExifInterface with the path to the image, or using the MediaStore.Images.Media database using the lat/lng columns.
You can never guarantee that you will always get location data for any photo. Providers (gps/wifi/cell) might be disabled, geo tagging might be disabled, and even if both are enabled, the phone may not be able to acquire a recent enough and accurate enough geo point.
When you are capturing an image through your mobile device, it usually use only the date and time of the captured moment to save the image on your mobile. If you want to add coordinates to the picture, you have to use the LocationManager class from Android during the capture. By this you can obtain the long/lat coordinates of a captured image.
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Please note that you have to include the next permission in the Android manifest file if you want to use the above snippet
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
read more here : android location strategies
Hi I am using this piece of code for camera in my activity.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PICTURE_WITH_CAMERA) {
if (resultCode == Activity.RESULT_OK) {
// To Write the Images in SDCard
File f = new File(imgName);
try {
Uri capturedImage = Uri
.parse(android.provider.MediaStore.Images.Media
.insertImage(getContentResolver(),
f.getAbsolutePath(), null, null));
Log.i("camera",
"Selected image: " + capturedImage.toString());
pic.setImageURI(capturedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
Log.i("Camera", "Result code was " + resultCode);
}
}
}
I am trying to apply the image captured to my ImageView. The first time, the camera takes a picture and sets it to the ImageView. But when I run it for the second time, my app is not able to set the newly captured image to the imageView. It crashes after returning from the camera intent. How can I solve this?
Any help is appreciated.
In order to avoid this kind of error, the bitmap has to be scaled first. I have tried the below piece of code and it works fine.
bm1=Bitmap.createScaledBitmap(bm, 300, 300,true);
pic.setImageBitmap(bm1);
bm.recycle();
bm=null;
When reading about Camera, I think it is Hardware. Hardware always is using its own Thread. Perhaps you need to create a memory copyied image and serve it by dispatcher. I do not know your code but I am interested in receiving it to help you.
http://blog.lieberlieber.com/2009/07/09/controlinvoke-wpf-style-and-special-bitmapsource-handling/
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f0c5e590-8bfe-4452-a784-4987af9fce89/
I am developing an app, where an image taken from the native camera app. is to be shown to the user.
The code I did is:
/* Intent */
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
/* Result */
onActivityResult(int requestCode, int resultCode,
Intent returnIntent) {
if(requestCode == TAKE_PICTURE) {
//picture from Camera
if (resultCode == Activity.RESULT_OK) {
if(returnIntent != null) {
try {
// Get the file path where the image is stored.
// This runs fine on all devices, except Samsung ones.
Uri selectedImage = returnIntent.getData();
if(selectedImage == null) {
if(returnIntent.getExtras() != null &&
returnIntent.getExtras().get(AppConstants.DATA) != null) {
// But, I get the image Bitmap here. Means the image is stored in disk.
Bitmap bmp = (Bitmap) returnIntent.getExtras().get(AppConstants.DATA);
}
}
} catch (Exception e) {
//
}
}
}
}
}
The problem here is, the above code works fine on all devices I tried (HTC's, SE's) but it somehow fails in the Samsung ones. The "Uri selectedImage = returnIntent.getData();" never returns anything. As my entire app is built over this logic of file path storing, I am not able to proceed.
Is there any solution people.
See Specify filename for picture to be taken for code to specify an EXTRA_OUTPUT parameter for the Intent which lets you specify a filename for the picture. Remember the filename when the activity result is called and use that if the intent.getData is NULL
And if you read the other comments in that bug report, you'll realize how many problems that picture taking on Android has.
Your best bet is to generate a file path you want to save the picture to and store it in a member variable. Then, when calling camera activity, put it (as Uri) in the MediaStore.EXTRA_OUTPUT extra.
Be sure to override onSaveInstanceState() and onRestoreInstanceState() and save/restore this path properly. This will make sure you'll still have it if the system decides to restart your activity in the meantime (which is very possible due to orientation changes and/or out of memory conditions).
here is the problem: i have searched for an answer for this and so far i made it work for the custom camera app that comes with htc phones.
i have the folowing
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) {
InputStream is=null;
File file=mInterface.getTempFile();
try {
is=new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(is==null){
try {
u = data.getData();
is=getContentResolver().openInputStream(u);
mInterface.saveStringPreferences(GlobalInterface.URI_SAVENAME, u.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//Now "is" stream contains the required photo, you can process it
setImage(is);
}
//and this is the code to the function that calls the intent:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(mInterface.getTempFile()));
startActivityForResult(intent, REQUEST_FROM_CAMERA);
the remaining functions getTempFile() is to create a temp file on /sdcard/blabla.tmp
and a function to set the captured image in an image view.
so the problem is that i tested this code on the custom camera on a lg phone and it crashes. the question is: is there a way to get an uri for the image location (i dont need a specific save location it could be the default set from the camera) and there were also some issues i came across which i dont need to solve like getting a smaller resolution image. i just need one image and to be able to set it in an imageView and the method to work for every camera app that is there, custom or native regardless.
is this possible or i need to create a new camera class for this? in my opinion the camera apps that come with the phone are more adjusted to the phone model so i would prefer using them against building a custom one.
tnx.
Try this:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Source