1

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;
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Alex
  • 29
  • 5
  • 1
    Your process may be terminated while it is in the background, particularly if a memory-hungry app, like a camera app, is in the foreground. What **exactly** is your problem? – CommonsWare Mar 20 '16 at 16:59
  • 1
    This question has an accepted answer: http://stackoverflow.com/questions/10319330/ondestroy-while-waiting-for-onactivityresult " A (Your activity ) will be re-created the next time the user visits/needs it, and at that point any pending results will be delivered" This Second answer on this one talks about saving the "photo path": http://stackoverflow.com/questions/16014930/android-activity-getting-destroyed-after-calling-camera-intent and the reasons that the activity is being destroyed. – Matthew Carlson Mar 20 '16 at 17:00
  • So, you want to say that when I call activityForResult, it will be called save it's data and when I start activity again the result will be ready ? If that's how it should be, it does not work for me, since I checked and my database is empty :( I took the photo and after destroying I started activity again, but nothing happened. – Alex Mar 20 '16 at 17:14
  • I found answer to similar question [HERE](http://stackoverflow.com/questions/16014930/android-activity-getting-destroyed-after-calling-camera-intent) – Gogi Bobina Apr 18 '16 at 15:22

2 Answers2

1

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.

Lovenkrands
  • 165
  • 1
  • 10
0

Make sure that the Orientation is not the problem here, in some devices the camera is landscape by default after onActivityResult called your Activity may restart.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Omar Mahmoud
  • 2,902
  • 1
  • 14
  • 22