Camera or Gallery intent destroys old activity on

2019-06-15 02:55发布

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?

4条回答
虎瘦雄心在
2楼-- · 2019-06-15 03:02
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

查看更多
The star\"
3楼-- · 2019-06-15 03:12

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.

查看更多
Lonely孤独者°
4楼-- · 2019-06-15 03:21

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.

查看更多
太酷不给撩
5楼-- · 2019-06-15 03:22

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/

查看更多
登录 后发表回答