Piotr
Piotr

Reputation: 1753

Camera or Gallery intent destroys old activity on some devices

I am working on app which uses WebView to display its content. However, it needs to open camera or gallery in order to choose picture:

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 1);

    Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent, 2);

It's working fine on most devices, but on HTC One and few others both intents destroys my activity, so webview is being reloaded while going back. I don't have noHistory flag in AndroidManifest.xml. What might be causing that issue? Can I avoid destroying my activity here?

Upvotes: 14

Views: 3688

Answers (4)

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);

By seeing your code I can judge that your motto is to capture the image and use it later on.

This is a known bug, The solution is that you need to create a separate folder for your application and before capturing you need to be sure that file is created and the same path you are giving to camera intent

Uri uriSavedImage=Uri.fromFile(new File("/sdcard/seperate/newImage.png"));
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("output", uriSavedImage);
startActivityForResult(cameraIntent, 1);

Reference: image from camera intent issue in android

Upvotes: 1

Sharad Mhaske
Sharad Mhaske

Reputation: 1103

If i m not wrong you are opening camera from device.Have you check that other app is not aquired the camera? you must acquire camera before starting camera activity may be some other app using camera instance.You must release the camera instance in on destroy or onstop method of activity so that next time it will available fr other app to use it or for your app to use.

Upvotes: 0

Nik
Nik

Reputation: 7214

It is normally, that Android kills your Activity when other app runs.

You must save Activity state in onSaveInstanceState and when activity will be recreated restore state in onRestoreInstanceState or in onCreate.

To restore state of WebView you may use cookies and sessions and save last opened url. When activity will be recreated just navigate WebView last saved url and process result from camera.

Upvotes: 7

Stephan Celis
Stephan Celis

Reputation: 2622

Maybe a stupid sugestion. But since it's destroyed, it means the device was low on memory.

If the only annoyance is that the webview reloads, maybe you can solve this by caching the content?

For example in the onStop() method of you activity get the content of the webview and store it somewhere. temporary file, sqlite,... . and in onCreate check if there is a cache (and maybe how old it is) and if needed put that in the webview.

Tutorial to get html code from webview: http://lexandera.com/2009/01/extracting-html-from-a-webview/

Upvotes: 0

Related Questions