Notification/Service - Could not copy bitmap to parcel blob - android

In my app I start a foreground service and create a notification for it:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
...
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon));
...
This throws an exception on one of my users devices like below (a java.lang.RuntimeException: Could not copy bitmap to parcel blob. exception).
My icons do have default dimensions and small sizes, don't know why they make problems on one device.
Does anyone know what the real issue could be? And how I could avoid this issue or gracefully handle this case? Seems to be a memory issue.
Mipmap icon
I have included following icons:
mdpi (48x48, 3,51kB)
hdpi (72x72, 6,65kB)
xhdpi (96x96, 9,08kB)
xxhdpi (144x144, 17,2kB)
xxxhdpi (192x192, 22,0kB)
Exception
java.lang.RuntimeException: Unable to start service com.my.app.services.OverlayService#ea0edcc with Intent { act=RESUME cmp=com.my.app/.services.OverlayService }: java.lang.RuntimeException: Could not copy bitmap to parcel blob.
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3079)
at android.app.ActivityThread.access$2200(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5585)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.RuntimeException: Could not copy bitmap to parcel blob.
at android.graphics.Bitmap.nativeWriteToParcel(Native Method)
at android.graphics.Bitmap.writeToParcel(Bitmap.java:1541)
at android.graphics.drawable.Icon.writeToParcel(Icon.java:705)
at android.os.Parcel.writeParcelable(Parcel.java:1437)
at android.os.Parcel.writeValue(Parcel.java:1343)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:711)
at android.app.Notification.writeToParcel(Notification.java:1731)
at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:701)
at android.app.NotificationManager.notify(NotificationManager.java:230)
at android.app.NotificationManager.notify(NotificationManager.java:194)
at com.my.app.services.BaseOverlayService.a(SourceFile:39)
at com.my.app.services.BaseOverlayService.a(SourceFile:23)
at com.my.app.services.OverlayService.onStartCommand(SourceFile:136)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3062)

Related

The app crashes when i pick image from gallery the second time. Have tried to call System.gc() to clear the memory, but didn't worked

2022-02-01 12:01:30.862 16616-16616/? E/ActivityThread: Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ChooserActivity$1#efec398 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ChooserActivity$1#efec398 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.(LoadedApk.java:1607)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1378)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1624)
at android.app.ContextImpl.registerReceiverAsUser(ContextImpl.java:1611)
at android.content.ContextWrapper.registerReceiverAsUser(ContextWrapper.java:720)
at com.android.internal.content.PackageMonitor.register(PackageMonitor.java:98)
at com.android.internal.content.PackageMonitor.register(PackageMonitor.java:86)
at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:523)
at com.android.internal.app.ChooserActivity.onCreate(ChooserActivity.java:828)
at android.app.Activity.performCreate(Activity.java:8127)
at android.app.Activity.performCreate(Activity.java:8098)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3513)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3700)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8061)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
2022-02-01 12:01:30.871 714-721/? E/statsd: Predicate 5980654721335871649 dropping data for dimension key (10)0x2010101->10036[I] (10)0x30000->launch[S]
2022-02-01 12:01:30.897 18012-18012/com.athermobileapp E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1250432)
2022-02-01 12:01:30.923 18012-18012/com.athermobileapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.athermobileapp, PID: 18012
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 1250432 bytes
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:161)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8061)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Caused by: android.os.TransactionTooLargeException: data parcel size 1250432 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(BinderProxy.java:593)
at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4647)
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)
at android.os.Handler.handleCallback(Handler.java:938) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:236) 
at android.app.ActivityThread.main(ActivityThread.java:8061) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 
You aren't running out of memory, so calling gc won't help (its also not reliable, and not necessary as Java will run gc if it needs to). Your problem is you're sending an intent that is too big. See the part of the crash that says android.os.TransactionTooLargeException: data parcel size 1250432 bytes? An intent's data can be a maximum of 1 MB. You're at about 1.2 MB. You need to decrease the amount of data in your intent.

why android asks for a huge memory allocation for a small photo?

I am trying to load a jpeg photo with a size of 965KB on my android phone. The code is below. When I run the code, the app crashes.
Button _btn = (Button) findViewById(R.id.btn1);
_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable _draw = getResources().getDrawable(R.drawable.sea, null);
}
});
This is the crash log.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.a3sumatch.multipart, PID: 29240
java.lang.OutOfMemoryError: Failed to allocate a 362797068 byte
allocation with 8388608 free bytes and 254MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at
android.content.res.Resources.loadDrawableForCookie(Resources.java:2761)
at android.content.res.Resources.loadDrawable(Resources.java:2654)
at android.content.res.Resources.getDrawable(Resources.java:833)
at
com.a3sumatch.multipart.MainActivity$1.onClick(MainActivity.java:40)
at android.view.View.performClick(View.java:5205)
at android.view.View$PerformClick.run(View.java:21176)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:5611)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
I am trying to load a jpeg photo with a size of 965KB on my android phone
That is an absolutely massive photo. JPEG, like PNG and WebP, is a compressed file format. To put that in perspective, a 3229x2480 photo of my balding head is 829.3KB in a JPEG. I would expect yours to be a bit larger in each dimension. That resolution is higher than the resolution of any Android device screen that I know of.
Your OutOfMemoryError is for a 362797068-byte allocation. A Bitmap is an uncompressed version of the image. 362797068 bytes is equivalent to about a 9524x9524 square image (and 4 bytes/pixel).
So, you need to do two things:
If you have this image in res/drawable/, move it to res/drawable-nodpi/
Reduce the resolution of this image by at least a factor of four along each dimension

TransactionTooLargeException when minimizing app

I am having a TransactionTooLargeException when I close/minimize my app. I am assuming it is due to my Bundle being too large (3 JSONArrays as strings), but it would be great if someone can diagnose more information from my error trace.
Here it is:
05-28 17:17:25.994 9518-9518/com.adamtrudeauarcaro.godbuilder E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 551100)
05-28 17:17:25.996 9518-9518/com.adamtrudeauarcaro.godbuilder D/AndroidRuntime: Shutting down VM
05-28 17:17:25.998 9518-9518/com.adamtrudeauarcaro.godbuilder E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.package.godbuilder, PID: 9518
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 551100 bytes
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4211)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: android.os.TransactionTooLargeException: data parcel size 551100 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:628)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:4132)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4203)
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6688) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
From your error trace it doesn't say, but I can tell you from experience certainly that this happens when your bundle is too large to store it as a saved instance state. Which means that when you minimize the app, it tries to call onSaveInstanceState and your bundle is storing more data than the supported by the system.
I encountered this exception before too. The scenario is we pass paths of all pictures or videos in album through Bundle to preview fragment, usually it is OK, but it becomes a problem when there are thousands of files in user's device. The exception will be thrown when activity stops and application is reclaimed by system, during which the argument data are passed to system through binder for restore later. Here is the exception trace: 
Thread Name: 'main'
Back traces starts.
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 2352912 bytes
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:161)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:263)
at android.app.ActivityThread.main(ActivityThread.java:8246)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
Caused by: android.os.TransactionTooLargeException: data parcel size 2352912 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(BinderProxy.java:568)
at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4429)
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)
... 7 more
Back traces ends.
To fix it, we restraint only one hundred paths around the current clicked picture or video. Here are the codes.
val selections = ArrayList<Selection>()
// The number of pictures in user device can be more than 1000.
// Restrict the list size to fix TransactionTooLargeException: data parcel size too large.
val interval = 100
val start: Int = max(0, position - interval / 2)
val end: Int = min(materials.size - 1, start + interval)
for (i in start..end) {
val albumItem: IAlbumItem = materials[i]
val selection = Selection(Triple("null", i, -1), Pair(-1, -1), albumItem)
selection.what = what
selections.add(selection)
}
val bundle = Bundle()
// The collection has changed, the position should offset the same too.
bundle.putLong("limitVideoDuration", limitVideoDuration)
bundle.putInt("position", position - start)
bundle.putSerializable("selections", selections)

Out of Memory : android.support.v7.app.AppCompatActivity.onCreate

we recently switched to using AppCompatActivity instead of FragmentActivity and now some devices are randomly crashing with the following error. No idea why this is happening.
Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 16008012 byte allocation with 4277152 free bytes and 4MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java)
at android.graphics.BitmapFactory.nativeDecodeAsset(BitmapFactory.java)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:703)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:733)
at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:779)
at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:741)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:2549)
at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:254)
at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:164)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:2549)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:2320)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4210)
at android.content.res.Resources.loadDrawable(Resources.java:4089)
at android.content.res.Resources.getDrawable(Resources.java:2005)
at android.content.res.Resources.getDrawable(Resources.java:1987)
at android.content.Context.getDrawable(Context.java:464)
at android.support.v4.content.ContextCompat.android.support.v4.content.ContextCompatApi21.getDrawable(SourceFile:3026)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(SourceFile:200)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(SourceFile:81)
at android.support.v7.app.AppCompatDelegateImplBase.(SourceFile:127)
at android.support.v7.app.AppCompatDelegateImplV9.(SourceFile:147)
at android.support.v7.app.AppCompatDelegateImplV11.(SourceFile:27)
at android.support.v7.app.AppCompatDelegateImplV14.(SourceFile:53)
at android.support.v7.app.AppCompatDelegateImplV23.(SourceFile:29)
at android.support.v7.app.AppCompatDelegate.create(SourceFile:203)
at android.support.v7.app.AppCompatDelegate.create(SourceFile:185)
at android.support.v7.app.AppCompatActivity.getDelegate(SourceFile:525)
at android.support.v7.app.AppCompatActivity.onCreate(SourceFile:74)
at com.dubizzle.horizontal.activities.AbstractActivity.onCreate(SourceFile:98)
at com.dubizzle.horizontal.activities.ItemPhotoViewer.onCreate(SourceFile:68)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
You are trying to load an image using the BitmapFactory right?
I assume that your image is too big:
16008012 byte allocation with 4277152 free bytes and 4MB until OOM
Your image is 16008012 but you only have 4277152 bytes free.
Try using a smaller image.
This happened to me when I set a rather large drawable as the windowBackground property of the activity's theme. Reducing the size of the image should fix it

java.lang.OutOfMemoryError: Failed to allocate a 756946956 byte allocation with 16777216 free bytes and 433MB until OOM

I want to change the background image of my Activity's layout every time I launch that activity. For that reason, inside onResume(), I am randomly setting the background image of layout like this:
#Override
public void onResume(){
super.onResume();
Random r = new Random();
int randNumber = r.nextInt(homeBackgroundImage.length);
mBackgroundLayout.setBackground(ResourcesCompat.getDrawable(getResources(), homeBackgroundImage[randNumber], null));
}
But, I am getting exception in the following line:
mBackgroundLayout.setBackground(ResourcesCompat.getDrawable(getResources(), homeBackgroundImage[randNumber], null));
This is the logcat output:
java.lang.OutOfMemoryError: Failed to allocate a 756946956 byte allocation with 16777216 free bytes and 433MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.Resources.getDrawable(Resources.java:806)
at android.support.v4.content.res.ResourcesCompatApi21.getDrawable(ResourcesCompatApi21.java:27)
at android.support.v4.content.res.ResourcesCompat.getDrawable(ResourcesCompat.java:60)
at in.avara.app.avaravrplayer.Activity.MainActivity.onResume(MainActivity.java:167)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
at android.app.Activity.performResume(Activity.java:6327)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Someone, please suggest me What is the optimized and best way to change background image of a layout randomly on every launch.
NOTE: I have already tried adding android:hardwareAccelerated="false" , android:largeHeap="true". But, it didn't work in my case.
Thanks in advance!
You are attempting to allocate 756946956 bytes ~= 721MB. This is much too large.
Moreover, 756946956 bytes represents the equivalent of a 13756 x 13756 pixel image. This is far larger than any Android screen.
You need to identify the image that is failing and reduce its resolution significantly.
If you put this image in res/drawable/, bear in mind that res/drawable/ is a synonym of res/drawable-mdpi/. Your image will be upsampled when you use that drawable on higher screen densities (e.g., xhdpi). You probably should move that drawable out of res/drawable/ and into either a density-specific directory for the desired density (e.g., res/drawable-xhdpi/), or into res/drawable-nodpi/ to disable this density-based resampling.
Try to put move your images from res/drawable/ to res/drawable/drawable-nodpi. If problem is still here, in your manifest under application put android:largeHeap="true".

Categories

Resources