加载完整的宽度和高度图像

Arc*_*pgc 5 android bitmap

<ImageView
    android:id="@+id/bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:src="@drawable/bg" />
Run Code Online (Sandbox Code Playgroud)

@ drawable/bg1600*1800(W*H)

我试过用

BitmapFactory.decodeResource(getResources(), R.drawable.bg, options);
Run Code Online (Sandbox Code Playgroud)

运用 inSampleSize

这是日志:

02-05 05:32:05.841: E/screenWidth in px =(876): 768
02-05 05:32:05.841: E/screenheight in px =(876): 1184
02-05 05:32:05.841: E/screenDensity =(876): 2.0
02-05 05:32:05.841: E/screenDensityDpi =(876): 320
02-05 05:32:05.841: E/decodeSampledBitmapFromResource reqWidth =(876): 768
02-05 05:32:05.851: E/decodeSampledBitmapFromResource reqHeight =(876): 1184
02-05 05:32:05.921: E/decodeSampledBitmapFromResource imageHeight =(876): 1800
02-05 05:32:05.921: E/decodeSampledBitmapFromResource imageWidth =(876): 1600
02-05 05:32:05.921: E/decodeSampledBitmapFromResource imageType =(876): image/jpeg
02-05 05:32:05.951: E/calculateInSampleSize: inSampleSize =(876): 1
02-05 05:32:09.291: E/dalvikvm-heap(876): Out of memory on a 44441616-byte allocation.
02-05 05:32:09.391: E/AndroidRuntime(876): FATAL EXCEPTION: main
02-05 05:32:09.391: E/AndroidRuntime(876): Process: com.example.splash, PID: 876
02-05 05:32:09.391: E/AndroidRuntime(876): java.lang.OutOfMemoryError
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:445)
02-05 05:32:09.391: E/AndroidRuntime(876):  at com.example.splash.SecondActivity.decodeSampledBitmapFromResource(SecondActivity.java:103)
02-05 05:32:09.391: E/AndroidRuntime(876):  at com.example.splash.SecondActivity.onCreate(SecondActivity.java:57)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.app.Activity.performCreate(Activity.java:5231)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.os.Handler.dispatchMessage(Handler.java:102)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.os.Looper.loop(Looper.java:136)
02-05 05:32:09.391: E/AndroidRuntime(876):  at android.app.ActivityThread.main(ActivityThread.java:5017)
02-05 05:32:09.391: E/AndroidRuntime(876):  at java.lang.reflect.Method.invokeNative(Native Method)
02-05 05:32:09.391: E/AndroidRuntime(876):  at java.lang.reflect.Method.invoke(Method.java:515)
02-05 05:32:09.391: E/AndroidRuntime(876):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-05 05:32:09.391: E/AndroidRuntime(876):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-05 05:32:09.391: E/AndroidRuntime(876):  at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)

对于高度为1184的设备而言,问题是

inSampleSize将为1(因此无变化)

因此,在完成所有这些过程并且发生相同的OOM时,它是无用的.

这个单一屏幕显示它需要44 MB的内存,我使用它:

long totalMem = Runtime.getRuntime().totalMemory();
Run Code Online (Sandbox Code Playgroud)

如果我将image src设置为@null,则上述值为3 mb

这是一种可靠的方法来查找应用程序的内存使用情况吗?

请帮助我如何在没有任何OOM的情况下加载这样的高分辨率图像.

Dig*_*tel 3

Picasso.with(context).load(R.drawable.bg).into(iv);
Run Code Online (Sandbox Code Playgroud)

使用 picaso 图像加载器来实现非常高效的链接