Search code examples
androidandroid-camera-intentstartactivityforresultandroid-ondestroy

Camera Intent destroys Activity


I am posting this question after several days of fighting with this problem. I have Floating Action Button and onClick listener calls takePhoto() method, where I obviously want to take a photo and save its Bitmap, which I will use later to save it in Database. But that's not the main problem. I start onActivityResult (...) to get data of taken photo. When I take photo and click save, the activity is simply destroyed (I used Log.v (...) to check if it actually is destroyed) I tried almost everything including overriding onSaveInstanceState (...) and so on. Here is my Android manifest file and part of that code.

Manifest:

<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;
}

Solution

  • I think it's the android:noHistory="true"... Looks like after taking the photo it returns to the previous activity on the history stack but there is no activity in there as it is set to keep no history.