Pressing back button stops app during Camera activity

Umit Kaya

I capture photo and save into my Gallery. Everything works perfect when I capture an image and save. But if the camera is on and if I press the back button without taking a picture then the application stops.

How I can solve this issue?

This is my code:

public class ImportCard extends Activity {

private static final int CAMERA_PIC_REQUEST = 1111;
ImageButton importimage;

@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import_card);

ImageButton importimage = (ImageButton) findViewById(R.id.importimage);
importimage.setOnClickListener(new OnClickListener(){   

@Override
public void onClick(View v) {

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);

        }
    });    
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
        //2
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  
        //3
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        //4
        File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
        try {
            file.createNewFile();
            FileOutputStream fo = new FileOutputStream(file);
            //5
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}

Android Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapcard"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera"
              android:required="true" /> 

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" /> 

<application
    android:allowBackup="true"
    android:icon="@drawable/vcard"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.mapcard.Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.mapcard.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.mapcard.NewCard"
        android:label="@string/title_activity_new_card" >
    </activity>
    <activity
        android:name="com.example.mapcard.ImportCard"
        android:label="@string/title_activity_import_card">
    </activity>
    <activity
        android:name="com.example.mapcard.CardBox"
        android:label="@string/title_activity_card_box" >
    </activity>
    <activity
        android:name="com.example.mapcard.QRCode"
        android:label="@string/title_activity_new_card" >
    </activity>
    <activity
        android:name="com.example.mapcard.Template"
        android:label="@string/title_template" >
    </activity>
    <activity
        android:name="com.example.mapcard.DisplayCard"
        android:label="@string/title_display_card" >
    </activity>
     <activity
        android:name="com.example.mapcard.GoogleMap"
        android:label="@string/title_activity_google" >
    </activity>
    <activity
        android:name="com.example.mapcard.ViewCard"
        android:label="@string/title_view_card" >
    </activity>
    <activity
        android:name="com.example.mapcard.QRScan"
        android:label="@string/title_activity_import_card" >
    </activity>
    <activity
        android:name="com.example.mapcard.ViewTemplates"
        android:label="@string/title_view_templates" >
    </activity>
    <activity
        android:name="com.example.mapcard.SendEmail"
        android:label="@string/title_send_email" >
    </activity>


</application>

Error in my LogCat:

java.lang.RuntimeException: Failure delivering result

Error on my device:

Unfortunately, App has stopped

Suchintya

You are checking only requestCode. You should check the value of resultCode also. When the user has successfully performed the action, the resultCode will be equal to RESULT_OK. If the user presses the back button then the resultCode will be RESULT_CANCELED. So your code must be modified like this.

protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
        //2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

going back to the parent activity on pressing app icon button

From Dev

going back to the parent activity on pressing app icon button

From Dev

Blank activity or fragment on reopening app after closing by pressing back button

From Dev

ANDROID: Activity state after pressing the back button

From Dev

App closing when pressing go back button

From Dev

When pressing the back button on the phone, the app minimizes

From Dev

App stops whenever I press back button

From Dev

Android activity go back to activity that started it instead of parent activity when pressing navigation bar back button

From Dev

Life cycle of Android Activity after pressing Back button

From Dev

Avoid splash screen activity when pressing Back button

From Dev

Don't kill Activity when pressing back button

From Dev

How to go to root activity from any fragment by pressing back button?

From Dev

How to go to root activity from any fragment by pressing back button?

From Dev

Pressing home button and going back into the app causes the app to crash

From Dev

Fragment pressing back button

From Dev

What is the effect of pressing the back button on the app's process in android?

From Dev

Android: app crashes after going back and pressing a button

From Dev

App closes on pressing back button when trying to move to previous fragment

From Dev

Android main activity's elements do not respond after pressing the back button from child activity

From Dev

ZXing QR code scanner embedded pressing back button during scan issue

From Dev

Pressing back button (backBarButtonItem) in KIF?

From Dev

AsyncTask nullPointerException pressing back button

From Dev

AsyncTask nullPointerException pressing back button

From Dev

Error when pressing back button

From Dev

How to go to previous selected tab when pressing an activity action bar back button

From Dev

refresh state on going to previous scene in react native from native (android) activity after pressing back button

From Dev

Application crashes when pressing back in camera

From Dev

When pressing home or back in app causes screen to flicker after activity closes

From Dev

how to stay in app while pressing back from activity which is called from notification?

Related Related

  1. 1

    going back to the parent activity on pressing app icon button

  2. 2

    going back to the parent activity on pressing app icon button

  3. 3

    Blank activity or fragment on reopening app after closing by pressing back button

  4. 4

    ANDROID: Activity state after pressing the back button

  5. 5

    App closing when pressing go back button

  6. 6

    When pressing the back button on the phone, the app minimizes

  7. 7

    App stops whenever I press back button

  8. 8

    Android activity go back to activity that started it instead of parent activity when pressing navigation bar back button

  9. 9

    Life cycle of Android Activity after pressing Back button

  10. 10

    Avoid splash screen activity when pressing Back button

  11. 11

    Don't kill Activity when pressing back button

  12. 12

    How to go to root activity from any fragment by pressing back button?

  13. 13

    How to go to root activity from any fragment by pressing back button?

  14. 14

    Pressing home button and going back into the app causes the app to crash

  15. 15

    Fragment pressing back button

  16. 16

    What is the effect of pressing the back button on the app's process in android?

  17. 17

    Android: app crashes after going back and pressing a button

  18. 18

    App closes on pressing back button when trying to move to previous fragment

  19. 19

    Android main activity's elements do not respond after pressing the back button from child activity

  20. 20

    ZXing QR code scanner embedded pressing back button during scan issue

  21. 21

    Pressing back button (backBarButtonItem) in KIF?

  22. 22

    AsyncTask nullPointerException pressing back button

  23. 23

    AsyncTask nullPointerException pressing back button

  24. 24

    Error when pressing back button

  25. 25

    How to go to previous selected tab when pressing an activity action bar back button

  26. 26

    refresh state on going to previous scene in react native from native (android) activity after pressing back button

  27. 27

    Application crashes when pressing back in camera

  28. 28

    When pressing home or back in app causes screen to flicker after activity closes

  29. 29

    how to stay in app while pressing back from activity which is called from notification?

HotTag

Archive