0

I'm trying to load & display 2 large images on an ImageView which is under a FrameLayout. But the app tends to crash. I'm able to load 1 image at a time but not 2. What I'm trying to do is basically display a map with clickable areas using this method: https://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/

I will implement the zoom and pan options in later. But for now I would just like to open up my app without it crashing.

Thanks!

Images dimensions (For both the images): 6027 x 4520

Here's the code: The XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/mainmap"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:src="@drawable/tc1"/>

<ImageView
    android:id="@+id/ident"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:src="@drawable/mask"
    android:visibility="invisible" />
</FrameLayout>

The Class:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class MainMap extends Activity {

ImageView a_ident, map; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    a_ident = (ImageView) findViewById(R.id.ident);
    map = (ImageView) findViewById(R.id.mainmap); 

    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.ident, option);

    BitmapFactory.Options option1 = new BitmapFactory.Options();
    option1.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.mainmap, option1);



}

}

2 Answers2

0

You should read the bitmap's dimensions and resize the bitmap depending on your screen's resolution. It is really unnecessary to display an image that has more pixels than the display. Here is the manual.

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

TpoM6oH
  • 8,385
  • 3
  • 40
  • 72
  • But I want to display a high resolution map of a certain area. And I don't want to lose out on quality. – user3662191 May 21 '14 at 19:11
  • Looks like you'll need to replace the first image with it's part with higher resolution on certain level of zoom, after recycling the first bitmap. It is simple impossible to have two bitmaps sized 6027 x 4520 loaded in the memory on majority of android devices today. – TpoM6oH May 21 '14 at 19:15
0

Your app is crashing due to an OutOfMemoryException. You will have to reduce the image resolution so it doesn't consume as much memory. Devices have a set amount of memory that is dedicated to dealing with Bitmaps; your app is clearly exceeding it. As TpoM6oH said, you should read the docs on how to properly manipulate Bitmaps.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53
  • The thing is that when I remove one ImageView from the XML and display only one, the app does not crash. But when I try loading 2 images, the app crashes. – user3662191 May 21 '14 at 19:13
  • Right, because the phone might have enough memory to deal with one image, but not both of them at the same time. – Emmanuel May 21 '14 at 19:15
  • Please mark this question as answered by selecting the answer that helped you the most. – Emmanuel May 21 '14 at 19:26