简体   繁体   中英

Setting a background for a relativelayout from the drawable image

What i am doing :: i am trying to load a image from drawable and set as a background of relativelayout in the adapter

What is happening ::

  • getting out of memory error
  • I tried scaling but many ways also still not able to resolve outof memory error
  • How to resolve this

AdptAtomicGodGallery.java

public class AdptAtomicGodGallery extends PagerAdapter {
    // Declare Variables
    Context context;
    Integer[] godImages;
    LayoutInflater inflater;
    String godTag;

    public AdptAtomicGodGallery(Context context, Integer[] _godImages, String _godTag) {
        this.context = context;
        this.godImages = _godImages;
        this.godTag=_godTag;
    }

    @Override
    public int getCount() {
        return godImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        // Declare Variables
        ImageView imgGodId;
        RelativeLayout relImgId;

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.adpt_atomic_god_gallery, container,false);

        // Locate the ImageView in viewpager_item.xml
        imgGodId = (ImageView) itemView.findViewById(R.id.imgGodId);
        relImgId = (RelativeLayout) itemView.findViewById(R.id.relImgId);


        /*Bitmap icon = BitmapFactory.decodeResource(context.getResources(),godImages[position]);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(icon, 100, 100, false);
        Drawable drawable = new BitmapDrawable(context.getResources(),resizedBitmap);
        relImgId.setBackgroundDrawable(drawable);
        relImgId.setTag(imgGodId);
        */

        try {
            Bitmap icon = BitmapFactory.decodeResource(context.getResources(),godImages[position]);

            ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
            icon.compress(CompressFormat.JPEG, 100, bos); 
            byte[] bitmapdata = bos.toByteArray();
            ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);

            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 8;
            Bitmap preview_bitmap=BitmapFactory.decodeStream(bs,null,options);
            Drawable drawable = new BitmapDrawable(context.getResources(),preview_bitmap);
            relImgId.setBackgroundDrawable(drawable);
            relImgId.setTag(imgGodId);
            bos.close(); 
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{

        }

        // Add viewpager_item.xml to ViewPager
        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((LinearLayout) object);
    }
}

Log:

02-15 00:04:42.402: E/AndroidRuntime(2616): java.lang.OutOfMemoryError
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:452)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:482)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at com.proj.adapters.AdptAtomicGodGallery.instantiateItem(AdptAtomicGodGallery.java:73)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:800)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.support.v4.view.ViewPager.populate(ViewPager.java:991)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.Choreographer.doFrame(Choreographer.java:544)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.os.Handler.handleCallback(Handler.java:733)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.os.Looper.loop(Looper.java:136)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at android.app.ActivityThread.main(ActivityThread.java:5086)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at java.lang.reflect.Method.invokeNative(Native Method)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at java.lang.reflect.Method.invoke(Method.java:515)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-15 00:04:42.402: E/AndroidRuntime(2616):     at dalvik.system.NativeStart.main(Native Method)

EDIT

BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),godImages[position]);
            Drawable drawable = new BitmapDrawable(context.getResources(),preview_bitmap);
            relImgId.setBackgroundDrawable(drawable);
            relImgId.setTag(imgGodId);

You seem to be doing a lot of unnecessary decoding.

First image is being created here:

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),godImages[position]);

Second image is being created here in the form of raw bytes (size of image is still the same):

ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
icon.compress(CompressFormat.JPEG, 100, bos); 
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);

Third image is being created here:

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(bs,null,options);

What you should be able to do instead is simply this:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),godImages[position], options);
relImgId.setImageBitmap(preview_bitmap);

In the background, the ImageView is actually going to create a BitmapDrawable which is backed by the Bitmap you supply it. You only have to decode the bitmap once.

You need to scale down your image.

Try this function for scaling:

public Bitmap decodeFile(String pathOfImage){
        File f = new File(path);
        if (!f.exists()){
            Log.d("Reh", "File doesn't exist");
        }
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //The new size we want to scale to
            final int REQUIRED_SIZE=70;

            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
                scale*=2;

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

And then simply:

      imageView.setImageBitmap(decodeFile("/sdcard/whatever your folder or file name is");

Or you can use: getAssets().open("...") to get image from assets folder. You might want to use your corresponding decode method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM