Android - App crashing due to memory error - android

I have an app that is not graphic intensive (as such). It has 2 Fragments, both with 2 GridViews inside. This is what I call Level-1 Activities because they are visible immediately when the app is opened.
Now the second GridView located inside Fragment #2 has 3 items, one of these Items opens a New Activity with a GridView inside it. This is what I call Level-2 Activities as they must be accessed by the user.
This second level GridView has 8 Image items. The Items are all identical size and shape, but the Images used for them are not, so they are scaled/resized etc. The Images are:
Image 1 - 465x232, 38kb
Image 2 - 512x251, 41kb
Image 3 - 900x379, 68kb
Image 4 - 630x258, 35kb
Image 5 - 700x346, 44kb
Image 6 - 615x409, 24kb
Image 7 - 800x383, 64kb
Image 8 - 400x169, 26kb
GridView XML
<GridView
android:id="#+id/essentials_gridview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:listSelector="#android:color/transparent"
android:numColumns="3"
android:padding="10dp"
android:stretchMode="columnWidth" />
GridView Item Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="125dp" >
<ImageView
android:id="#+list/grid_item_image_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" />"
<TextView
android:id="#+list/grid_item_image_text"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:background="#50000000"
android:singleLine="true"
android:ellipsize="end" />
</RelativeLayout>
The Error I get is:
08-10 10:09:42.060: E/dalvikvm-heap(18982): Out of memory on a 9023376-byte allocation.
This happens when I'm moving back and forth between the Level 1 and Level 2 activities. After couple of times, it crashes with that error.

Seems you have a memory leak, I suggest you to watch this session:
Google I/O 2011: Memory management for Android Apps
In my own experience, I recommend you to implement ViewHolder pattern inside your Gridview's adapters.

I suspect the problem is related to image handling. You could try to use sampled version of the original if they are big. You could also cache the image that will be memory efficient. When you are navigating between the activities the images are loading again and again and consuming the memory.
The following link could be useful here. If you post your sample java code where you are loading image then it will be possible to pin point your issue.
http://developer.android.com/training/displaying-bitmaps/index.html
http://developer.android.com/training/displaying-bitmaps/manage-memory.html

Related

Android SeekBar Doesn't Move Smoothly

I'm trying to write a Camera app in android. I wanted to use a SeekBar for zoom in/zoom out of the camera. Here goes my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<SeekBar android:id="#+id/sliderZoom"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
/>
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:background="#000000">
<ImageButton
android:layout_height="50dp"
android:layout_width="wrap_content"
android:src="#drawable/flash"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:onClick="onFlashClick" />
<ImageButton
android:layout_height="50dp"
android:layout_width="wrap_content"
android:src="#drawable/zoom"
android:scaleType="fitXY"
android:layout_centerInParent="true"
android:onClick="onZoomClick" />
</RelativeLayout>
</LinearLayout>
If I remove either of the ImageButton, my SeekBar moves smoothly. But it doesn't move smoothly if I've both of the ImageButton. I tried to run the app both in portrait and landscape mode. The problem remains same in both mode.
How can I solve it?
EDIT: As per some comment, I feel to add some more information. Right now there is no event handler for the SeekBar. It doesn't zoom in or zoom out the camera. It is just one simple SeekBar' with maximum value set to some random integer. For my case I tried 5 to 500. In every case I've the problem. By smooth move I mean when I try to slide it with touch it should move easily, should not halt. If there is one ImageButton, I can slide the SeekBar pointer left/right without any problem. But if there are two ImageButton, the pointer doesn't move that easily. It looks like each time the the pointer is moved by one unit, some time consuming operation runs on the UI thread which halts the UI from capturing touch event. Though the fact is there is no time consuming operation in UI thread. There is not a single function written for any type of computation. I didn't even add the camera preview in the FrameLayout or camera isn't initialized
More Edit: I just found out one interesting turn of event. If the image source is same image(doesn't matter which one) for both ImageButton, then it works perfectly. But if I've two different image for two ImageButton, then it starts causing the problem. I hope this new information will help to get some idea of what is happening. The resolution of the images are 1299x1299 and less than 40kb in size.
Actually this is not a problem. Camera zoom level is not so much. Basically zoom level is 5-7. So, as you have used SeekBar as your zoom controller it means your seekbar's max level is 7(suppoose). For this reason, it buffer per level when you pull the bar.
For your test, you can set value seekBar.setMax(30) and then pull your seekbar. I hope you will get that smooth

Simple way to create display a few dozen icons?

In my app, I want to have some activities that display a series of small images, I'm aiming for 3 per row in portrait and 5 in landscape, but I'm not rigid about those numbers.
The first problem is, I can't figure out a layout, or a configuration of a layout that will allow me to achieve this effect properly. I've tried TableLayout, FrameLayout and RelativeLayouts, but none of them make my images look nicely arranged in both orientations.
In this case the images are flags. I've tried creating 9 Patches out of the ones with symbols in the middle which I don't want to be distorted. Android Studio doesn't pick up that they are .9 files. But that is a separate issue.
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/iceland"
android:layout_weight="0.25"
android:scaleType="fitXY"
android:id="#+id/imageView"/>
I made a mockup of what I'm trying to achieve. I'd really really appreciate any tips you guys can offer. Thanks!
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
taken from this link should deliver an example, that could be useful to you.

Changing the Size of my ListView by one Pixel messes up Formatting

Let me show you two links to demonstrate my point:
http://i.snag.gy/QP1i2.jpg (The ListView is 60 pixels)
http://i.snag.gy/DvXsL.jpg (The ListView is 61 pixels)
The whole file is done with an outer Vertical LinearLayout to provide weighted percentages (e.g. the ListView is 75%, the search part is 4%, so on and so forth) independent of device screen size. Within that outer LinearLayout I have a nested Layout (Linear or Relative) depending on my needs for the particular row.
It works beautifully, except for the last part. And it seems a lot of the items I try adding end up messing the formatting so I'm not sure if the problem is how I'm doing the ListView. Anyways, here's the relevant code:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:background="#null"
android:layout_weight="0.75"
android:paddingLeft="0dip" >
<ImageView
android:id="#+id/searchdivider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:contentDescription="#string/desc"
android:src="#drawable/searchdivider" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/searchdivider" >
</ListView>
</RelativeLayout>
Thank you for your help!
EDIT: I should add that I don't want the height to be 60dp, obviously. I want it to fill_parent; however I picked the arbitrary value of 60 and 61 to figure out exactly what change makes the layout mess up.
2nd EDIT: I think I figured out the problem. It seems when I make the Theme AppCompat, it looks and acts fine. But when I make the Theme NoTitleBar (with or without fullscreen) then it acts all screwy. I haven't changed it in the manifest but rather the place in the graphical layout that lets you modify it for that one activity only.
Any suggestions?
android:gravity="left" on imageView and android:gravity="right" on the other listView

Missing or Incorrect images and backgrounds randomly throughout app lifecycle

I was hoping someone here might have an idea what causes this sort of behaviour:
Throughout my application, in seemingly random places and in random conditions I'm observing this strange UI issue. Images are on occasion being loaded black (with the correct bounds) or with the incorrect image source (again, with the correct bounds). This effects ImageViews and has effected android:background tags with references to colour resources.
My application relies on 6 library projects, it runs Native Code via a Service and Activities in the App use GlSurfaceViews (although, not all Activities which display the problem contain OpenGL components). The problem could I suppose be from any of these places or a combination of them through using large amounts of memory.
You can see this behaviour in the following screen shots:
This is actually a 6 or so pixel wide column separator image which has been incorrectly drawn into my ImageView (the ImageView seems to have correctly sized itself).
When going out of the Application and then back in again (repeatedly) it instead appeared (and remained) like so:
After a Force Clear and a Clear App Data it returned to the correct format:
As you can also see the Magnifying Glass image next to it is displaying fine in each of these. The problems with these missing/incorrect images and backgrounds seems to happen randomly, throughout the application lifecycle, and I've been unable to find a way of reproducing it.
The layouts for these images are nothing special, I'm not doing anything funny during the rendering lifecycle (i'm not overriding onDraw() or onMeasure() or the like). The source of these images aren't being set dynamically but via the XML.
As you can see from the above example, it's not a build issue as it occurs between app lifecycles not between installs. It's also happening on different devices, Samsung 8.9, Acer Iconia Tab, Motarola XOOM,
It seems to me to be some sort of error with the reference table, could it perhaps have been nudged by my native code? Or is it an effect of me in some stages of the application using too much memory?
Here's the XML source for the above example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/browseProgressWrapper"
android:layout_width="match_parent"
android:layout_height="#dimen/actionbar_compat_height"
android:orientation="horizontal">
<RelativeLayout android:layout_width="#dimen/search_bar_width"
android:layout_height="match_parent">
<EditText android:id="#+id/browseFilter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:imeOptions="actionSearch"
android:background="#drawable/edit_text_blue"
android:maxLength="30"/>
<ImageView android:id="#+id/clearSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_input_delete"
android:layout_marginRight="5dp"/>
</RelativeLayout>
<ImageView android:id="#+id/browseFilterButton"
android:src="#drawable/ic_menu_search"
android:scaleType="center"
android:layout_width="#dimen/actionbar_compat_height"
android:layout_height="#dimen/actionbar_compat_height"
android:layout_gravity="center_vertical"
android:minWidth="#dimen/actionbar_compat_height"/>
</LinearLayout>
A more full description of the code / layout surrounding another such occurrence I happened to get the screenshot for:
I have a "Settings" Activity which restarts my app after saving new settings details. It does this by stopping a Service, calling a new Activity (the Splash Activity) and finishing itself:
mConfiguration.save();
mConfiguration = new Configuration(Configuration.getInstance());
getActivity().stopService(new Intent(getActivity(), NativeService.class));
getActivity().finish();
startActivity(new Intent(getActivity(), SplashActivity.class));
Most of the time (and on most devices) this works fine, the Splash Activity contains an image which loads correctly. Sometimes though on some devices the Splash Activity loads either an incorrect resource (what my testers refer as "an upside down Nike tick") or just a blank box (as seen below). Does anyone know why?
Here is the Layout for the Splash page, as you can see it's pretty simple, no surprises:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/ContentBackgroundColor"
android:orientation="vertical" >
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/manager_android_400" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ProgressBar
style="#android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
</LinearLayout>
Theory tested and debunct:
I've theorised that this could be a processor/memory issue where the Layout isn't being drawn fully before the Splash screen exits and moves onto the next Activity so I put in this piece of code:
image = (ImageView) findViewById(R.id.image);
image.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
image.getViewTreeObserver().removeGlobalOnLayoutListener(this);
moveToStartScreen.start();
}
});
The hope was the code above would make sure the Image is definitely loaded before moving onto the Start page but seems to have had no observable effect.
Another Theory
I was also wondering if this could be being caused by the R.id / R.colour / R.drawable resources some how being currupted in program execution? Does anyone know why that might happen.
Could my native code be running rampant on some memory addresses that Android isn't correctly allocating?
Has anybody noticed this before - or perhaps know why this behaviour occurs?
Graeme, I had almost the same problem and found out that it was a reported bug of the android plattform. It was corrected in the 3.0 version I think. Which API are you compiling with? Try to build with the last available api and be sure to compile with JDK 1.6
If your problem is related to this bug, this should fix the problem.
This is a simple problem of refresh, clean, and rebuild.
Images in your various drawable folders or resource id indices are out of sequence because they were either changed outside of the eclipse IDE (via external source control such as GIT, SVN or other edits) and not refreshed in the eclipse navigator. Or, the files may have been updated in a library project upon which your UI Activity depends.
I have found that although .java file dependencies are propagated throughout the system, this is not always the case for resources such as images and .xml files.
The solution is fairly simple, clean everything, refresh all of your projects, and rebuild. The stretched or black edges should be gone.
Note: The predominant manifestation of this problem occurs when 9-patch images become treated like standard .png images. This means that they get stretched in a linear manner across the image instead of just at the edges. To me, this explains your 'Torn/Stretched' example. I have seen similar often. Another common manifestation is that text strings occasionally get displayed with the wrong resources!

Listview item background image problem in my android application

I have in my application a listview with an adapter that uses different layouts for the items.
I want the result to be like in the attached exp_result.png.
http://imageshack.us/photo/my-images/717/expresult.png/
But unfortunatly - I get like in result.png.
http://imageshack.us/photo/my-images/839/resultf.png/
The problem is that image stretches on the screen not the way I wanted.
Any ideas?
Maybe other solution to this layout - maybe built in?
Drawables are in the links, since I cannot upload images. The original picture is like in the first listview item in the exp_result.png. I want to use 1 picture for each - top, buttom and middle and to be used no matter the listview item size. XML is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_up" >
<TextView
android:id="#+id/name_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<TextView
android:id="#+id/number_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
</LinearLayout>
I can really really use some help here.
Yoav
This may be a good application of the 9-patch image type:
9-Patch

Categories

Resources