开发者

ImageView Android RAM

开发者 https://www.devze.com 2023-02-17 05:55 出处:网络
A followup from my original question. Is there a way to use ImageViews in android apps without using a lot of RAM?

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开发者_JAVA技巧 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消