Reputation: 4623
Following is my imageview. I am using it in listview but app is crashing at startup.When I set layout width and height to wrap_content it is working but big images are shown as property is wrap_content. Please help
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imgView"/>
Following is 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)
Upvotes: 0
Views: 175
Reputation: 11766
You have an out of memory exception,I dont know the exact value,but when you load a bitmap in to the memory it takes the space like width*height*3 MB due to loading large bitmaps in memory like this the app runs out of memory and crashes,You may think that the reducing the size of the ImageView may fix this issue. But NO! that's only the size of the View not the bitmap.What you want to do is to somehow scale your image and then load it to the ImageView or use views like RecycleView in your app
Use the following code snippets for compressing the image,source: Displaying Bitmaps Efficiently
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);
}
Calculating sample size:
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;
}
In your case if you want to load the file image1.jpg to an ImageView
of size 50x50 call the function like
Bitmap b=decodeSampledBitmapFromFile(path,50,50)
and set it to imageView like:
ImageView.setImageBitmap(b);
Upvotes: 1
Reputation: 967
if you are using raw images
check whether height and width of image used to set in this ImageView is too large. if its size is large use image with small size.
When large images are set on small ImageViews
it causes outofmemmory error.
Use this link to resize images to http://romannurik.github.io/AndroidAssetStudio/index.html
Upvotes: 0