Camera Intent会破坏活动

时间:2016-03-20 16:57:30

标签: android android-camera-intent startactivityforresult android-ondestroy

经过几天与这个问题的斗争,我发布了这个问题。我有浮动操作按钮和onClick监听器调用takePhoto()方法,我显然想要拍照并保存其位图,稍后我将用它将其保存在数据库中。但这不是主要问题。我开始onActivityResult(...)来获取拍摄照片的数据。当我拍照并单击保存时,活动被简单地销毁(我使用Log.v(...)来检查它是否实际被销毁)我几乎尝试了所有内容,包括覆盖onSaveInstanceState(...)等等。这是我的Android清单文件和该代码的一部分。

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alexparunov.collegemascots" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".account.SignIn"
              android:label="@string/title_activity_login">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".account.SignUp"
              android:label="@string/title_activity_register"/>
    <activity
        android:name=".profile.MainProfile"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenOrientation="portrait"
        android:noHistory="true"/>
</application>

Java:private static final int REQUEST_IMAGE_CAPTURE = 1;

private void takePhoto(){
    if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},1);
    }
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if(cameraIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
    }
    Image finalImage = new Image();
    if(user != null && imageBitmap != null) {
        finalImage.setImageOwner(user.getUsername());
        finalImage.setImageName(generateName());
        finalImage.setImage(ImageUtils.getBytesFromBitmap(imageBitmap));

        try {
            ImageDatabase imageDatabase = new ImageDatabase(this);
            imageDatabase.open();
            finalImage.setImageId(imageDatabase.createImage(finalImage));
            Log.v("TakeImage",""+finalImage.getImageId());
            imageDatabase.close();
            images.add(finalImage);
            user.setImages(images);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        Bundle extras = data.getExtras();
        imageBitmap = (Bitmap) extras.get("data");
    }
}

private String generateName(){
    String name = user.getName()+"_";
    if(images != null) {
        name += images.size() + ".png";
    }
    return name;
}

2 个答案:

答案 0 :(得分:0)

确保方向不是问题所在,  在某些设备中,默认情况下,在onActivityResult调用您的Activity后,相机可能会重新启动。

答案 1 :(得分:0)

我认为它是android:noHistory =&#34; true&#34; ...看起来像拍照后它返回到历史堆栈上的上一个活动,但那里没有任何活动它被设定为没有历史。