如何使用数组/位图控制图像的大小?

时间:2018-09-25 21:24:25

标签: android android-studio android-bitmap

错误 enter image description here

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

我的解决方案 这是通过“相对布局”中的一个图像视图链接到布局的:

enter image description here

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)

也请注意,即使没有解决方案,我也会收到相同的错误。

1 个答案:

答案 0 :(得分:0)

  

无法分配具有 8584576 个空闲字节的 19133580字节分配   和 8MB 直到OOM

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

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