Tapping camera save button action opening again camera in android jellybean - android

i'm trying to take picture and save it to a file by using startActivityForResult(). But after tapping "Save" button it did not save anything and again opening the camera.
Using device Samsung Galaxy S3(4.1.1) and Samsung Galaxy Nexus(4.1.1) but its woking fine with Motorola Defy(2.3.4).
Is it the issues with Android OS 4.1.1 or Device ?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File file = null;
Date date = new Date();
try {
file = new File("photosearch-"+date.getTime()+".jpeg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
Log.e(TAG, " startActivityForResult");
startActivityForResult(intent, CAMERA_ACTIVITY);
} catch (Exception e) {
Log.d(TAG, ""+e);
//Check if sdcard is accessible
Toast.makeText(getActivity(), "Unable to access SD Card", Toast.LENGTH_LONG).show();
getActivity().finish();
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult....");
if(resultCode == Activity.RESULT_CANCELED){
Intent intent = new Intent(getActivity(), AnotherListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
getActivity().finish();
}
if(resultCode == CAMERA_ACTIVITY){
Log.d(TAG, "It should come here..");
}
}

you have problem in you Extra Parameter, use this example here

Related

resultCode == RESULT_OK is not working in redmi note3

Hi I want to capture image and do some opration for that image in my next activity.I have tried to run the below code in Samsung and Moto G and its working fine.When I tried same code with redmi note3,after Clicking right mark it is coming to the same activity.How to solve this? This is my code:
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputFileUri = Uri.fromFile(originalFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "No camera app found!", Toast.LENGTH_LONG).show();
}
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
Bitmap imageData = null;
if (resultCode ==Activity.RESULT_OK) {
try
{
BitmapFactory.Options bitopt=new BitmapFactory.Options();
imageData = BitmapFactory.decodeFile(imagePath, bitopt);
Uri tempUri = getImageUri(getApplicationContext(), imageData);
Intent i = new Intent(this, Image_R.class);
i.putExtra("imageUri", tempUri.toString());
startActivityForResult(i, 3);
}
catch (Exception e) {
Toast.makeText(this, "Unable to save image", Toast.LENGTH_LONG).show();
}
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
}
}
replace this:--
i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);
Add this code to your activity tag inside your Manifest file.
android:configChanges="orientation|keyboardHidden|screenSize"
It will not let your current activity to destroy and re-create so you will get the result.
And if this doesn't work make sure that your Device is not on power/battery saving mode.
RedMi devices with active power/battery saving mode cause previous activity to lose it's state and when coming back for result it will call onCreate again so you won't get the true result.

How to open a system settings activity?

I am trying to open this class on the click of a button, it works fine, however, this class is not present in HTC devices, so i want my app to show a toast on this exception, but it doesn't show anything-
Intent intent = new Intent();
intent.setAction("android.media.action.DISPLAY_AUDIO_EFFECT_CONTROL_PANEL");
if ((intent.resolveActivity(getPackageManager()) != null)) {
startActivity(intent);
} else {
Toast.makeText(getBaseContext(), "you are offline", Toast.LENGTH_LONG).show();
// No equalizer found :(
}
You can open with
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
You can return by pressing back button on device.
Try this
In your MainActivity.java put
Intent intent = new Intent();
intent.setAction("android.media.action.DISPLAY_AUDIO_EFFECT_CONTROL_PANEL");
if ((intent.resolveActivity(getPackageManager()) != null)) {
// here is the changes
// REQUEST_CODE is an any integer value
startActivityForResult(intent, REQUEST_CODE);
} else {
Toast.makeText(getBaseContext(), "Unable to open setting..", Toast.LENGTH_LONG).show();
}
If you want to handle result of an intent then
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// add your code here
}

onActivityResult() only triggers when Camera is cancelled

I'm having some trouble using the Camera intent in Android. I want to be able to take
a picture and to use the picture later (retreiving the URI). Here's the code:
newPicBut.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
PhotoChoiceActivity.this.launchCameraIntent();
}
});
public void launchCameraIntent()
{
//create camera intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//set fileUri
MediaManager manager = new MediaManager();
manager.setPictureName(currentItemToAdd.toString());
intent.putExtra(MediaStore.EXTRA_OUTPUT, manager.getOutputMediaFileUri());
startActivityForResult(intent, 100);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 100){
if(resultCode == RESULT_OK){
Toast.makeText(getApplicationContext(), "Image saved to: " + data.getData(), Toast.LENGTH_LONG).show();
}
}
}
So basically the button calls the launchCameraIntent() method. The MediaManager creates a file, and I use the file URI in the intent.putExtra to indicate where the file should be saved. The Camera intent is called, but here is the problem: the onActivityResult() only gets triggered when I cancel my camera intent. So it doesn't get triggered at all when I accept the picture that I've taken.
Hope you can help!

How to use android Device native camera..?

I am developing an app where i want to use device native camera.i.e. i want to use the exact camera which the device will show when we click on camera option from device including widgets like front camera option etc. i saw one app in android market which exactly doing this. Can any one help me??
Thanks,
Ram.
If u want to save the image in a folder, then this is the easiest way:
try {
//where to save the image
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "foldername" + File.separator);
root.mkdirs();
sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
startCameraActivity();
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
finish();
}
protected void startCameraActivity() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 101);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==101 && resultCode==-1)
{
try
{
//use that file here
}
catch(Exception ex)
{
Log.v("OnCameraCallBack",ex.getMessage());
}
}
If u want to save it in default location:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 101);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==101 && resultCode==-1)
{
try
{
//use that file here
}
catch(Exception ex)
{
Log.v("OnCameraCallBack",ex.getMessage());
}
}

Camera intent problem, camera is starting without request

I have a little problem with my camera intent. As I know, when camera orientation is changed, activity is restarted. Okej, I am using the code bellow.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (myApplication)getApplication();
if(savedInstanceState ==null ) getFullImage(null);
else{
String somevalue = savedInstanceState.getString("uri");
getFullImage(somevalue);
}
}
private void getFullImage(String testValue)
{ if(testValue == null){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(), UUID.randomUUID()+ ".jpg");
outputFile = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFile);
startActivityForResult(intent, TAKE_PICTURE);
}else
{
outputFile = null;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(testValue);
outputFile = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, testValue);
startActivityForResult(intent, TAKE_PICTURE);
finishFromChild(getParent());
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_CANCELED) {
Log.i(TAG, "Back Button");
finishFromChild(this);
}
else
if(requestCode == TAKE_PICTURE && resultCode == RESULT_OK)
{
//I'm creating new file here (for this question is irelevant)
} catch (IOException e) {
e.printStackTrace();
}
Intent myIntent = new Intent(getBaseContext(), com.test.activities.SaveFileActivity.class);
myIntent.putExtra("image", newPath);
startActivityFromChild(this, myIntent, SAVE_ITEM);
finishFromChild(this);
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("uri",outputFile.getPath());
}
After picture is captured, I press DONE button and I go to SaveFileActivity. Everyting works fine, until I try from SaveFIleActivity to go to another activity, then camera is starting again. Where should I look for the problem ? Maybe I should kill camera intent, but when ?
I suspect you want to be using the simpler startActivity() and finish() methods instead of startActivityFromChild() and finishFromChild(). I admit, however, that I'm a bit unclear as to what the use of the ones you are actually for.

Categories

Resources