camera request no activityresult - android

My camera doesn't seem to come back from taking a picture where I think it should
I fire up an activity like this:
String name = mNameText.getText().toString();
File imgFile = new File(Environment.getExternalStorageDirectory () + "/gradeBook/" + name + ".jpg");
String fileName = imgFile.getAbsolutePath();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fileName)));
startActivityForResult(intent, REQUEST_FROM_CAMERA);
but my onActivityResult never gets called. I've set breakpoints throughout and never once does
the method even get entered... it's just bypassed completely.
Anyone know any reason why it wouldn't get called and what I could do to make it happen?

Related

Android taking camera return empty intent

I am developing android app and my app have button to take camera. Previously i had fallen into a state where return data in onActivityResult after taking picture being null. This is camera expected behaviour whereby if we put EXTRA_OUTPUT in intent , it would return null. For that reason , I did null checking code and it went fine .
Now again after a few days and i tested . I still fallen into same issue again. But this time data is not null. data has empty intent such as intent and data.getData() become null.I fixed this by checking data.getData() == null and it works again. I don't why it is like that. Just curious about what was going on. For that reason i have to re-upload to production again. :-(
//camera intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra("requestCode", Constants.REQUEST_IMAGE_CAPTURE);
Intent chooseImageIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
chooseImageIntent.setType("image/* video/*");
chooseImageIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
chooseImageIntent.putExtra("requestCode", Constants.REQUEST_CHOOSE_FROM);
//app can use camera
if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
//add output file path which camera will save image to
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Helpers.getOutputMediaFileUri());
//create choose
Intent chooser = Intent.createChooser(chooseImageIntent, "Select From");
//add take camera intent as first intent
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,new Intent[]{takePictureIntent});
//open up dialog
((Activity) mContext).startActivityForResult(chooser, Constants.REQUEST_CHOOSE_FROM);
} else {
((Activity) mContext).startActivityForResult(chooseImageIntent, Constants.REQUEST_IMAGE_GALLERY);
}
EDITED
I know I how to fix the problem. What i don't understand is return data must be null if i put in EXTRA_OUTPUT. Mostly importantly the code I implemented few weeks back , i am quite sure that data return null and suddenly it is non null value again.
Actually the camera intent doesnot return the data in intent because after getting image it kill the activity.
so try this
void opencameraForPicture(int requestCode, Uri fileUri) {
checkPermissionForMarshMello(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE);
Intent intent = new Intent(Constants.CAMERA_INTERNAL_CLASS);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
/* start activity for result pass intent as argument and request code */
startActivityForResult(intent, requestCode);
}
/**
* This method set the path for the captured image from camera for adding
* the new picture in the list
*/
private Uri getOutputMediaFile() {
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory(), "."
+ Constants.CONTAINER);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
File mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + System.currentTimeMillis() + ".png");
Uri uri = null;
if (mediaFile != null) {
uri = Uri.fromFile(mediaFile);
}
return uri;
}
in #Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String imagePath = fileUri.getPath();
//you can decode this path as bitmap
}

Getting photo information from camera intent

I am having a very difficult time with the camera intent and trying to get data about the photo that was just taken
I launch the camera like this
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File output = new File(dir, "IMG_" + timeStamp + ".jpg");
photoUri = Uri.fromFile(output);
startActivityForResult(camera, CAMERA_REQUEST);
in my onActivityResult the intent is null so I cannot use anything there to get the Uri but doing this.
Bitmap photo = MediaStore.Images.Media.getBitmap(getContentResolver(),photoUri);
gets the photo just fine. What I want to do is get information about the photo Latitude,Longitude,time taken etc so I tried this
MediaStore.Images.Media.query(getContentResolver(),photoUri,null,null,null,null);
but the cursor is always returned as null so it obviously cannot find the uri. It seems that the image is not getting out into the MediaStore provider. How can I get the data from the photo just taken?

onActivityResult getting called as soon as camera intent is sent

I am using the camera intent to launch the camera in my app but as soon as the intent gets fired the onActivityResult gets fired and I have not even taken a picture yet.
When I do take a picture, select it and return back to my activity the onActivityResult does not get called at all
here is how I launch the camera
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Mobile Map");
if (!tempDir.exists()) {
if (!tempDir.mkdir()) {
Toast.makeText(this,
"Please check SD card! Image shot is impossible!",
Toast.LENGTH_SHORT).show();
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(new Date());
File mediaFile = new File(tempDir.getPath() + File.separator+ "IMG_" + timeStamp + ".jpg");
photoUri = Uri.fromFile(mediaFile);
camera.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(camera, CAMERA_REQUEST);
} else {
Toast.makeText(this,"This device does not have a rear facing camera",Toast.LENGTH_SHORT).show();
}
Why is the onActivityResult only getting called after the camera intent launches?
The problem was that in my manifest I had the activity set to singleInstance and apparently startActivityForResultdoes not like that

Image not appearing in Gallery

I am using this code to take the picture from the camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
File storagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/");
if(!storagePath.isDirectory()){
storagePath.mkdirs();
}
File myImage = new File(storagePath,
Long.toString(System.currentTimeMillis()) + ".jpg");
Uri fromURI=Uri.fromFile(myImage);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fromURI);
startActivityForResult(intent,PulseConstants.CAMERA_REQUEST_CODE);
The image is stored correctly with the given file name in "My Files" folder of the phone...But when I open Gallery of the phone, this image does not appear ??
Please let me know if I am doing anything wrong??
TIA,
VijayRaj
This may have to do with the fact that the Android media scanner is not constantly indexing files on the phone. In the past I have called the following function immediately after saving an image to notify the MediaScanner:
private void scanMedia(File file) {
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(scanFileIntent);
}
Hope this works!

Android Camera for Picture and Video

I want to start camera activity in my android app and I know how to do that. I want to ask when the camera activity finishes, how can I check as if it was the picture or the video taken by the user?
UPDATED
I have a dialog where it asks 2 things.
New Photo or Video
Existing Photo or Video
If it's no. 1, it means camera will be started and user can either take a picture or the video and it will return to the activity.
If it's no.2, it mean gallery will be started having pictures and videos for a user to select one and will return back to the activity.
Hello Umair,
I have done this type of application I searched many time but I didn't get any proper solution so I changed your my menu & they are now
1)Take New Photo
2)Take New Video
3)Existing Image/Video
Process will be like this
1)I use an global variable
2)So when user click on menu one I sets global variable value to 1
3) Start the activity for result like below
try{
System.gc();
String fileName = System.currentTimeMillis()+".jpg";
String mPathImage = Environment.getExternalStorageDirectory()+ "/" + fileName;
File file = new File(mPathImage);
Uri outputFileUri = Uri.fromFile( file );
Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mIntent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult(mIntent, 1);
mValue=1;
}catch(Exception e){
}
If User click on menu 2 I change value of global variable to 2
& starts the activity for result like below.
try {
System.gc();
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 1);
mValue=2;
}catch(Exception e){}
If user click on 3rd menu I set value to 3
& start the activity for result like below.
try{
System.gc();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent,1);
mValue=3;
}catch(Exception e){}
}
This will show all the images & video's in mobile
Then finally when activity gets closed use global variable to check whether user want to new image or video or existing image/video.
Hope this will help you..

Categories

Resources