我想以编程方式在ImageView上设置图像, 但是当我尝试放一个jpg文件时,我收到一个错误。 这是我的代码:
VideoView videoview;
ImageView immagine;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
immagine = (ImageView) findViewById(R.id.imageView2);
immagine.setImageResource(R.drawable.aresbkg);
这就是逻辑:
Process: com.lorenzofusinato.b_happy.cambiobutton, PID: 5481
java.lang.OutOfMemoryError: Failed to allocate a 715478412 byte allocation with 2982288 free bytes and 379MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
at android.content.res.Resources.getDrawable(Resources.java:767)
at android.content.Context.getDrawable(Context.java:525)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:358)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:85)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:93)
at com.lorenzofusinato.b_happy.cambiobutton.activity_main.onCreate(activity_main.java:31)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
我该如何解决这个问题?这不是我第一次使用ImageView,因此我不知道为什么在这种情况下我会收到此错误。在我的其他应用程序中一切正常。 非常感谢。 洛伦佐
答案 0 :(得分:2)
使用此library来避免OOM。
答案 1 :(得分:1)
一个很好的旧OutOfMemory:无论如何,我认为2.5 MB对于在Android设备上展示来说太大了。
也许您可以使用像毕加索(http://square.github.io/picasso/)这样的库来调整图像大小? (就像@Devit 951首先提出的那样)