Question

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

Was it helpful?

Solution

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

OTHER TIPS

Add

android:noHistory="false"

in AndroidManifest.xml for that activity

In onActivityResult you should check whether thumbnail is null, and if so just return.

Edit: like this

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ...
    Bundle extras = data.getExtras();
    if (extras == null || extras.get("data") == null) return;

    Bitmap thumbnail = (Bitmap) extras.get("data");
    ...  
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top