首页 > 解决方案 > 如何使用数组/位图控制图像的大小?

问题描述

错误 在此处输入图像描述

我有一个向左滑动的图片库,但现在我得到了如上面屏幕截图所示的错误。

我的解决方案这与相对布局中具有一个图像视图的布局相关联:

在此处输入图像描述

public class SlideImage extends AppCompatActivity{


    ImageView bitmapIV;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slide_image_layout);


        bitmapIV = (ImageView)  findViewById(R.id.imageView);
        Drawable drawable = this.getResources().getDrawable(R.drawable.chevrolet_equinox_2001_0);
        Bitmap bitmap =((BitmapDrawable) drawable).getBitmap();
        bitmapIV.setImageBitmap(bitmap);

        bitmapIV = (ImageView)  findViewById(R.id.imageView);
        Drawable drawable1 = this.getResources().getDrawable(R.drawable.chevrolet_equinox_2001_1);
        Bitmap bitmap1 =((BitmapDrawable) drawable).getBitmap();
        bitmapIV.setImageBitmap(bitmap);



        bitmapIV = (ImageView)  findViewById(R.id.imageView);
        Drawable drawable2 = this.getResources().getDrawable(R.drawable.chevrolet_equinox_2001_2);
        Bitmap bitmap2 =((BitmapDrawable) drawable).getBitmap();
        bitmapIV.setImageBitmap(bitmap);


        bitmapIV = (ImageView)  findViewById(R.id.imageView);
        Drawable drawable3 = this.getResources().getDrawable(R.drawable.chevrolet_equinox_2001_3);
        Bitmap bitmap3 =((BitmapDrawable) drawable).getBitmap();
        bitmapIV.setImageBitmap(bitmap);


        bitmapIV = (ImageView)  findViewById(R.id.imageView);
        Drawable drawable4 = this.getResources().getDrawable(R.drawable.chevrolet_equinox_2001_4);
        Bitmap bitmap4 =((BitmapDrawable) drawable).getBitmap();
        bitmapIV.setImageBitmap(bitmap);




    }
}

LOGCAT

09-25 17:30:25.534 2582-2582/com.example.batyaa.firstgearautomotive_2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.batyaa.firstgearautomotive_2, PID: 2582
    java.lang.OutOfMemoryError: Failed to allocate a 19133580 byte allocation with 8584576 free bytes and 8MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
        at android.content.res.Resources.loadDrawable(Resources.java:2540)
        at android.content.res.Resources.getDrawable(Resources.java:806)
        at android.content.Context.getDrawable(Context.java:458)
        at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:463)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
        at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
        at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:86)
        at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:94)
        at com.example.batyaa.firstgearautomotive_2.cars.chevrolet_equinox_2001.sliderAdapter_ChevroletE.instantiateItem(sliderAdapter_ChevroletE.java:63)
        at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1010)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1224)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
        at android.support.v4.view.ViewPager$3.run(ViewPager.java:273)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:603)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

还要注意,即使没有我的解决方案,我也会遇到同样的错误。

标签: androidandroid-studioandroid-bitmap

解决方案


未能分配19133580 字节分配,其中8584576字节和8MB直到 OOM

就像我猜的那样,Bitmap您尝试打开使用的文件大小是 8MB,这对于 Android 上的文件来说太大了。

相反,通过在线网站减小Bitmap文件大小或调整文件大小,然后就可以了。


推荐阅读