jamesmontemagno / MediaPlugin

Take & Pick Photos and Video Plugin for Xamarin and Windows
MIT License
712 stars 359 forks source link

[Android] Activity is killed on low RAM devices. #286

Open zanesc opened 7 years ago

zanesc commented 7 years ago

Bug Information

Version Number of Plugin: 2.7.0.50 - beta Device Tested On: Asus Memo Pad 7 ME176C, Samsung I8200 Galaxy S III mini VE Version of VS: 7.0.1 (build 24) Mac Version of Xamarin Forms: 2.3.4.247

Steps to reproduce the Behavior

Used this plugin to take a photo async using the camera and then display it on the screen.

Expected Behavior

User takes picture, accepts the picture, and it shows up on the screen.

Actual Behavior

User takes picture, accepts the picture, and the app restarts. Seems to be a memory issue on phones with 1gb ram. Are these phones simply not supported? No crash is logged as the app technically doesn't crash, the memory is just freed and the app restarts.

Code snippet

         await CrossMedia.Current.Initialize();
        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            await DisplayAlert("No Camera", ":( No camera available.", "OK");
            return;
        }

        var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            Directory = "Images",
            Name = "Image.jpg",
            CompressionQuality = 50,
            PhotoSize = Plugin.Media.Abstractions.PhotoSize.Custom,
            CustomPhotoSize = 20
        });

        if (file == null)
            return;

        image.Source = ImageSource.FromFile(file.Path);
jamesmontemagno commented 7 years ago

Can you grab the logcat/device logs and put it in http://gist.github.com

jamesmontemagno commented 7 years ago

I can validate randomly on my device that is 512MB that I just bought. It is hard to say, basically Android is killing the activity: https://stackoverflow.com/questions/16014930/android-activity-getting-destroyed-after-calling-camera-intent

However I totally follow this procedure. It is like it is killing the full app instead of just my activity perhaps. Which is weird. I don't know if this is a regression or always been this way, it is hard to say as I turned off all compression and jazz like that and still happens. However sometimes I can take 10 photos and sometimes 2.

zanesc commented 7 years ago

Sorry I have been busy lately. I can say it may not be directly RAM related. Maybe the CPU on these lower powered device might be the culprit?

I have tried it on 3 phones with 1GB RAM with varying results. The better the CPU the better it worked with the Moto G (3rd gen) working flawlessly with 1GB of RAM.

None of the device are mine however so if you still need the logcat I will have to borrow the phones again to get that for you.

DeanClaes commented 7 years ago

I can confirm the same behaviour on my device. I have tried it on my Samsung Galaxy J7 (2016), this device has 2 GB RAM and I have even put android:largeHeap="true" in the manifest but to no avail. It's always restarting the activity.

Edit: Turns out there is a setting under the developer options called "Do not keep activities". Turning this off has fixed the issue for me.

cjlotz commented 7 years ago

We've also seen this happening on low-end devices (1 GB or less) via HockeyApp crash reports out in the field. The crash logs include a call to Bitmap.CreateBitmap at the top of the stack. From looking at the source code this seems to happen when you cater for rotation of the photo on the device. I'll check to see if I can get a sample stack trace.

cjlotz commented 7 years ago

Are you aware of the ExifInterface Support Library. I see they provide a stream based approach to work with Uri's which might help?

jamesmontemagno commented 7 years ago

Nice! We have a binding too: https://www.nuget.org/packages/Xamarin.Android.Support.Exif/25.3.1

I will do this in next update!

jaime-marcondes commented 7 years ago

Are this Issue available in 3.0.1 version ? If not, do you plan to release soon ?

Thanks!

NisuSan commented 7 years ago

So, issue have been solved in 3.0.1 version? On the LG g3, what I'm use for testing, I have 3 from 5 crashes app. If issue don't was fixed, can you show how to fix this from code?

Brosten commented 6 years ago

I just tried this on a Samsung Galaxy S5. Still restarts after the 3.1.2 update =(

Brosten commented 6 years ago

After setting MaxWithHeight to about 2500 and compression to 80 it works alot better for me.

sinappsys commented 6 years ago

Hi, Are there any news? I tried with version 3.1.3 and crashes on devices with little RAM.

PaulVrugt commented 6 years ago

@jamesmontemagno any progress on this? We too have devices that experience this behavior

FilipeTabosa commented 6 years ago

Any news about it? I am still having trouble with some phones (including moto C plus which has 1gb ram). I tried to use App Center in order to track this error, but it does not log any crash. Analyzing the ANR from my phone I could find the following code:

"HeapTaskDaemon" daemon prio=5 tid=7 Blocked | group="system" sCount=1 dsCount=0 obj=0x12c221f0 self=0xaee06d00 | sysTid=1722 nice=0 cgrp=default sched=0/0 handle=0x97b30920 | state=S schedstat=( 898231237 141411457 350 ) utm=54 stm=35 core=0 HZ=100 | stack=0x97a2e000-0x97a30000 stackSize=1038KB | held mutexes= at dalvik.system.VMRuntime.runHeapTasks(Native method)

Not sure if this error has anything to do with the application error, but it may be helpful.

UPDATE:

So I decided to take a look on the logcat right after the application crashes, and it shows the following error:

04-30 14:02:56.177 876 2993 W ActivityManager: Duplicate finish request for ActivityRecord{bb7f6fc u0 com.srp/md5dff1bed22da39876f271fd39214b8f60.MediaPickerActivity t1032 f} 04-30 14:02:56.177 876 2993 I ActivityManager: Force finishing activity ActivityRecord{dd06ead u0 com.mediatek.camera/com.android.camera.CaptureActivity t1032 f} 04-30 14:02:56.177 876 2993 W ActivityManager: Duplicate finish request for ActivityRecord{dd06ead u0 com.mediatek.camera/com.android.camera.CaptureActivity t1032 f} 04-30 14:02:56.178 268 453 D AALService: enableAALEvent: 0 -> 1 04-30 14:02:56.187 876 3413 D DisplayManagerService: Display listener for pid 22838 died. 04-30 14:02:56.187 876 2987 W ActivityManager: Unable to launch app com.srp/10142 for provider com.srp.fileprovider: launching app became null 04-30 14:02:56.187 22599 22599 E ActivityThread: Failed to find provider info for com.srp.fileprovider 04-30 14:02:56.188 22599 22599 W CamAp_PhotoMode: IOException, when doAttach 04-30 14:02:56.188 22599 22599 W Util : [closeSilently]c is null,return.

JaccoDieleman commented 6 years ago

Same problem here on a Samsung Galaxy S4. The problem is easy to reproduce with the option "Do not keep activities" turned on.

I see no other solution than saving the state of my mainActivity when it is being destroyed by the platform. The state should include the path of the photo. When the activity is resumed I'll have to start processing the photo.

The above solution involves a lot of work for me. Did anybody else solved this in some other way?

nikkilocke commented 6 years ago

I am getting this issue on a Nokia 5 (1.9GB RAM, according to Developer Options/Memory). It means my app is completely flaky, and can't be released. I have already spent 3 days trying to find out why this is happening (I thought it was a problem in the app). Is there any solution, or alternative library I can use?

nichojo89 commented 6 years ago

This can be replicated with using an LG G6 device on Oreo, and using Jame's GitHub project. Hopefully this can be fixed soon or else we will also have to migrate away.

hponaganti commented 5 years ago

On Android Samsung Galaxy S5 and few of my clients who uses LG and Samsung S8 reported the same issue. I was able to replicate the issue clearly on Galaxy S5 and there is no hockey crash. Media Plugin version is "4.0.1.5"

nikkilocke commented 5 years ago

My app is now a lot less flaky. I did 2 things - I asked for a smaller size picture, and I made absolutely sure I disposed of all the disposable objects as soon as possible. It might be worth checking for disposable objects left undisposed before blaming the plug in.

JaccoDieleman commented 5 years ago

I thought I had this problem but it was caused by orientation changes. For some reason the camera app on my Galaxy S4 (android 4.4) causes the orientation to change. This orientation change caused my app to restart.

Solution: indicate that your app can handle orientation changes itself. You can do this in the Activity attribute of the MainActivity.

Maybe this helps some of you.

hponaganti commented 5 years ago

I thought I had this problem but it was caused by orientation changes. For some reason the camera app on my Galaxy S4 (android 4.4) causes the orientation to change. This orientation change caused my app to restart.

Solution: indicate that your app can handle orientation changes itself. You can do this in the Activity attribute of the MainActivity.

Maybe this helps some of you.

Yes it happens with orientation changes

and these are the settings on my activity [Activity(Name = PackageName + ".MainActivity", Exported = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden, LaunchMode = LaunchMode.SingleTask)]

federicojoselucia commented 4 years ago

Any news about this issue?

taublast commented 4 years ago

Just an idea, what if you do a GC.Collect() before creating the camera intent?.. Another obvious move is just to capture photo with a newly created built-in custom camera control, instead of using system activity, this way we would have total control over ram usage and the main activity will not go to background allowing android to kill it.

federicojoselucia commented 4 years ago

As it was mentioned before "Do not keep activities" developer option reproduces the issue. Well, aparently our tester did have this turned on by mistake so just turning it off solved the issue.

tsafadi commented 4 years ago

Any news about this? I updated to AndroidX but some of my clients still have problems with it on low RAM devices.

rihadavid commented 4 years ago

If you want to fix the issue for yourself, download the repo, add it to your solution, reference the project instead of the nuget (in all your projects) and in Android/MediaPickerActivity.cs, remove the code in OnDestroy().

Also, don't forget that when you restore your activity after being killed by the system (can be reproduced by DKA developer option), you can have a different Activity/Page/ViewModel instance, so if you want to show the image in the UI, you might need to pass it to the new instance.

Removing the code in OnDestroy() might not be the best solution, so I am not making a PR, but it fixes this issue. You can see further discussion on the problem at #855, if anyone wants to make a proper fix, that would be great.

Mitbhatt07 commented 4 years ago

Any help or workaround would be greatly appreciated on this issue. We are having a lot of complaints due to this issue. Thank you.

rihadavid commented 4 years ago

Any help or workaround would be greatly appreciated on this issue. We are having a lot of complaints due to this issue. Thank you.

Workaround is in my comment right above yours.

tsafadi commented 4 years ago

If you want to fix the issue for yourself, download the repo, add it to your solution, reference the project instead of the nuget (in all your projects) and in Android/MediaPickerActivity.cs, remove the code in OnDestroy().

Also, don't forget that when you restore your activity after being killed by the system (can be reproduced by DKA developer option), you can have a different Activity/Page/ViewModel instance, so if you want to show the image in the UI, you might need to pass it to the new instance.

Removing the code in OnDestroy() might not be the best solution, so I am not making a PR, but it fixes this issue. You can see further discussion on the problem at #855, if anyone wants to make a proper fix, that would be great.

So for you the odd behavior is gonna and the app doesn’t freeze anymore after removing the code from the OnDestroy() method? Do you know what specifically causes this behavior inside the method?

tsafadi commented 4 years ago

@jamesmontemagno is there any solution or a way to solve this error? We are having some clients complain that it freezes and then the app restarts. I know this has been an issue for quite some time now. I don't know what to tell the clients because I don't know the roots of the error. Do you have any idea why this is happening? I have everything up to date using AndroidX...

thanderOliveira commented 4 years ago

I commented on a issue that was already closed, so I will just post it here:

James, where do I check that "Don't Keep Activities" settings?

My clients are facing that same issue on asus smartphones, but I was not able to simulate. The app doesnt crash but it restarts while taking photo and after clicking OK.

The smartphone has 2gb of RAM and the storage is empty, so it doesnt look like a RAM or storage issue. Android version is 8.0 and 9.0 ASUS_A001D ASUS_X00RD

My Activity is as follows: [Activity(Label = "Auvo", Icon = "@drawable/icon_roxo", Theme = "@style/MainTheme", MainLauncher = false, ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

benizz0 commented 4 years ago

Activity OnPause was triggered when use this plugin to take a photo async using the camera. Use it for preventing data lost or call GC.Collect inside

mclucky commented 3 years ago

The crash scenario after taking pictures is still an issue on some android devices -> https://github.com/jamesmontemagno/MediaPlugin/issues/900 https://github.com/jamesmontemagno/MediaPlugin/issues/901

Is there any solution in progress?

dev-thinks commented 3 years ago

After taking pictures, it is NOT working in android 10, but works fine 8, 9. any workaround?

Getting unauthorizedException when taking photo in android 10 camera (Samsung Note 9).