2

A followup from my original question.

Is there a way to use ImageViews in android apps without using a lot of RAM?

In the original question I found that my app used a lot of RAM, and found that it was the ImageViews which I used that too up all the RAM.

To get the Image I use

public void addPictureOnClick(View view){
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
    } 

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            // currImageURI is the global variable I'm using to hold the content:// URI of the image
            Uri currImageURI = data.getData();                
            image.setImageURI(currImageURI);
        }

I would like to know if there is another way to show pictures rather than the URI?

Community
  • 1
  • 1
Bastaix
  • 835
  • 3
  • 12
  • 23
  • How large are the images you're loading, how many, and what's the RAM usage when you're using them compared to when you're not? – DMags Mar 17 '11 at 14:59
  • @DMags: I don't know the size. It is pictures taken with the phone camera. The RAM usage is 4Mb without one picture and 24Mb with picture – Bastaix Mar 17 '11 at 15:05

1 Answers1

4

You can scale your pictures :

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
imageViewFIeld.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length, options));

The scaling process could be longer if you have big images, but the memory load will be lower on display.

Edit: More you increase the inSampleSize value, more times it take, less memory usage you will have.

inSampleSize documentation : If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. Also, powers of 2 are often faster/easier for the decoder to honor.

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize

Hrk
  • 2,725
  • 5
  • 29
  • 44
  • And one thing more about SampleSize is that it should in multiple of 2 and sample of 12 will give you some image. I don't think you are going to see any thing with this sample size that is 2. – AZ_ Mar 17 '11 at 15:53
  • Powers of 2 is recommanded but not mandatory as in the documentation "Also, powers of 2 are often faster/easier for the decoder to honor." If you use 2 as sample size, you will have an half size of the image: original 1024x768, scale 2: 512x384 – Hrk Mar 17 '11 at 16:35
  • 2
    It may also be useful to use decodeInbouds to obtain the size of the image, then dynamically adjust the inSampleSize, allowing you to obtain the optimal resolution of the image. – smith324 Mar 18 '11 at 01:57