以下是我的imageview。我在listview中使用它但是应用程序在启动时崩溃。当我将布局宽度和高度设置为wrap_content时它正在工作但是大图像显示为属性是wrap_content。请帮忙
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imgView"/>
以下是logcat:
12-29 17:47:24.803 15424-15424/? D/dalvikvm: Not late-enabling CheckJNI (already on)
12-29 17:47:24.843 15424-15424/com.example.parsaniahardik.listviewwithimage E/Trace: error opening trace file: No such file or directory (2)
12-29 17:47:24.884 15424-15424/com.example.parsaniahardik.listviewwithimage W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
12-29 17:47:24.884 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
12-29 17:47:24.884 15424-15424/com.example.parsaniahardik.listviewwithimage W/dalvikvm: VFY: unable to resolve interface method 14515: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
12-29 17:47:24.884 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
12-29 17:47:24.884 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
12-29 17:47:24.884 15424-15424/com.example.parsaniahardik.listviewwithimage W/dalvikvm: VFY: unable to resolve interface method 14519: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
12-29 17:47:24.884 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
12-29 17:47:24.924 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
12-29 17:47:24.924 15424-15424/com.example.parsaniahardik.listviewwithimage W/dalvikvm: VFY: unable to resolve virtual method 14415: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
12-29 17:47:24.924 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: VFY: replacing opcode 0x6f at 0x0007
12-29 17:47:24.924 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
12-29 17:47:24.924 15424-15424/com.example.parsaniahardik.listviewwithimage W/dalvikvm: VFY: unable to resolve virtual method 407: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-29 17:47:24.924 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-29 17:47:24.924 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
12-29 17:47:24.924 15424-15424/com.example.parsaniahardik.listviewwithimage W/dalvikvm: VFY: unable to resolve virtual method 429: Landroid/content/res/TypedArray;.getType (I)I
12-29 17:47:24.924 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-29 17:47:25.044 15424-15429/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_CONCURRENT freed 195K, 3% free 8160K/8391K, paused 14ms+0ms, total 36ms
12-29 17:47:25.083 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_FOR_ALLOC freed 7K, 3% free 8159K/8391K, paused 4ms, total 4ms
12-29 17:47:25.083 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm-heap: Grow heap (frag case) to 9.831MB for 1920012-byte allocation
12-29 17:47:25.104 15424-15429/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_CONCURRENT freed <1K, 3% free 10034K/10311K, paused 0ms+0ms, total 6ms
12-29 17:47:25.154 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_FOR_ALLOC freed 0K, 3% free 10034K/10311K, paused 0ms, total 5ms
12-29 17:47:25.154 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm-heap: Grow heap (frag case) to 13.950MB for 4320012-byte allocation
12-29 17:47:25.173 15424-15429/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_CONCURRENT freed 0K, 2% free 14253K/14535K, paused 14ms+1ms, total 18ms
12-29 17:47:25.253 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_FOR_ALLOC freed 1875K, 15% free 12385K/14535K, paused 3ms, total 5ms
12-29 17:47:25.253 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm-heap: Grow heap (frag case) to 15.125MB for 3145740-byte allocation
12-29 17:47:25.284 15424-15429/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_CONCURRENT freed <1K, 13% free 15456K/17671K, paused 18ms+0ms, total 24ms
12-29 17:47:25.414 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_FOR_ALLOC freed <1K, 13% free 15456K/17671K, paused 10ms, total 10ms
12-29 17:47:25.433 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm-heap: Grow heap (frag case) to 21.875MB for 7077900-byte allocation
12-29 17:47:25.454 15424-15429/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_CONCURRENT freed 0K, 10% free 22368K/24647K, paused 16ms+1ms, total 21ms
12-29 17:47:25.574 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_FOR_ALLOC freed 3072K, 17% free 20503K/24647K, paused 5ms, total 6ms
12-29 17:47:25.574 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm-heap: Grow heap (frag case) to 22.690MB for 2764812-byte allocation
12-29 17:47:25.604 15424-15429/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_CONCURRENT freed <1K, 6% free 23202K/24647K, paused 20ms+1ms, total 24ms
12-29 17:47:25.643 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_FOR_ALLOC freed 1200K, 11% free 22009K/24647K, paused 7ms, total 7ms
12-29 17:47:25.643 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm-heap: Grow heap (frag case) to 24.525MB for 3145740-byte allocation
12-29 17:47:25.664 15424-15429/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_CONCURRENT freed <1K, 10% free 25081K/27783K, paused 14ms+1ms, total 19ms
12-29 17:47:25.713 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_FOR_ALLOC freed 0K, 10% free 25081K/27783K, paused 4ms, total 4ms
12-29 17:47:25.713 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm-heap: Forcing collection of SoftReferences for 7077900-byte allocation
12-29 17:47:25.723 15424-15424/com.example.parsaniahardik.listviewwithimage D/dalvikvm: GC_BEFORE_OOM freed 9K, 10% free 25071K/27783K, paused 10ms, total 10ms
12-29 17:47:25.733 15424-15424/com.example.parsaniahardik.listviewwithimage E/dalvikvm-heap: Out of memory on a 7077900-byte allocation.
12-29 17:47:25.733 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: "main" prio=5 tid=1 RUNNABLE
12-29 17:47:25.733 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: | group="main" sCount=0 dsCount=0 obj=0xb4f3e4b0 self=0xb8c804e0
12-29 17:47:25.733 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: | sysTid=15424 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1207602112
12-29 17:47:25.733 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: | schedstat=( 0 0 0 ) utm=39 stm=6 core=0
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.content.res.Resources.loadDrawable(Resources.java:1930)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.content.res.Resources.getDrawable(Resources.java:659)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.support.v7.widget.TintManager.getDrawable(TintManager.java:175)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.support.v7.widget.TintManager.getDrawable(TintManager.java:168)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:51)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at com.example.parsaniahardik.listviewwithimage.CustomListAdapter.getView(CustomListAdapter.java:46)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.AbsListView.obtainView(AbsListView.java:2251)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.ListView.onMeasure(ListView.java:1156)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.View.measure(View.java:15172)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.View.measure(View.java:15172)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.View.measure(View.java:15172)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.View.measure(View.java:15172)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.View.measure(View.java:15172)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.View.measure(View.java:15172)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.View.measure(View.java:15172)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.Choreographer.doFrame(Choreographer.java:525)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.os.Handler.handleCallback(Handler.java:615)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.os.Handler.dispatchMessage(Handler.java:92)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.os.Looper.loop(Looper.java:137)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at android.app.ActivityThread.main(ActivityThread.java:4745)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at java.lang.reflect.Method.invokeNative(Native Method)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at java.lang.reflect.Method.invoke(Method.java:511)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage I/dalvikvm: at dalvik.system.NativeStart.main(Native Method)
12-29 17:47:25.743 15424-15424/com.example.parsaniahardik.listviewwithimage A/libc: Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 15424 (stviewwithimage)
答案 0 :(得分:1)
你有一个内存不足的例外,我不知道确切的值,但是当你将位图加载到内存中时,由于加载大而需要像 width * height * 3 MB 这样的空间内存中的位图如此应用耗尽内存并崩溃,您可能认为减小ImageView的大小可能会解决此问题。 但是没有!这只是视图的大小而不是位图。您要做的是以某种方式缩放图像,然后将其加载到ImageView或使用像 RecycleView 在您的应用中
使用以下代码段来压缩图片,来源:Bayoz Swipe Menu
public static Bitmap decodeSampledBitmapFromFile(String path,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
计算样本量:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
在您的情况下,如果您要将文件 image1.jpg 加载到 50x50 的ImageView
,请调用此函数
Bitmap b=decodeSampledBitmapFromFile(path,50,50)
并将其设置为imageView,如:
ImageView.setImageBitmap(b);
答案 1 :(得分:0)
如果您使用原始图像
检查用于在此ImageView中设置的图像的高度和宽度是否太大。如果它的大小很大,请使用小尺寸的图像。
如果在小ImageViews
上设置大图像,则会导致默认错误。
使用此链接将图片大小调整为http://romannurik.github.io/AndroidAssetStudio/index.html