在相机活动期间,按后退按钮可停止应用

时间:2014-01-08 10:17:24

标签: android android-activity android-camera back-button

我拍摄照片并保存到我的图库中。当我捕捉图像并保存时,一切都很完美。但是如果相机处于打开状态,如果我按下后退按钮而不拍照,则应用程序停止。

我如何解决这个问题?

这是我的代码:

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清单文件:

<?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>

我的LogCat出错:

  

java.lang.RuntimeException:传递结果失败

我的设备出错:

  

不幸的是,App已停止

3 个答案:

答案 0 :(得分:7)

您只检查requestCode。您还应该检查resultCode的值。当用户成功执行操作时,resultCode将等于RESULT_OK。如果用户按下后退按钮,则resultCode将为RESULT_CANCELED。所以你的代码必须像这样修改。

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

答案 1 :(得分:1)

添加

android:noHistory="false"
该活动的AndroidManifest.xml中的

答案 2 :(得分:-1)

onActivityResult中,您应该检查thumbnail是否为空,如果是,则返回。

编辑:像这样

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");
    ...  
}

}