Reputation: 949
I have created a GridView
as following
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="application.example.com.fragment_practice.MainActivity"
tools:showIn="@layout/app_bar_main">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="@dimen/grid_view_width"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/dp_10" />
</RelativeLayout>
Following is my MainActivity.java
class
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
}
Following is ImageAdapter.java
class
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Constructor
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(180, 180));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(15, 15, 15, 15);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6,
R.drawable.sample_7, R.drawable.sample_8,
R.drawable.sample_13, R.drawable.sample_9,
R.drawable.sample_10, R.drawable.sample_11,
R.drawable.sample_12, R.drawable.sample_13,
R.drawable.sample_14, R.drawable.sample_16,
R.drawable.sample_16, R.drawable.sample_17,
R.drawable.sample_18, R.drawable.sample_19,
R.drawable.sample_20, R.drawable.sample_21,
R.drawable.sample_22, R.drawable.sample_23
};
}
Every thing is working perfect until I scroll.
Following is the Exception I am getting on scrolling the GridView
3-4 time up and down.
FATAL EXCEPTION: mai
Process: application.example.com.fragment_practice, PID: 18917 java.lang.OutOfMemoryError: Failed to allocate a 4915212 byte allocation with 2666656 free bytes and 2MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2230) at android.content.res.Resources.loadDrawableForCookie(Resources.java:4284) at android.content.res.Resources.loadDrawable(Resources.java:4158) at android.content.res.Resources.getDrawable(Resources.java:2043) at android.content.res.Resources.getDrawable(Resources.java:2025) at android.content.Context.getDrawable(Context.java:464) at android.widget.ImageView.resolveUri(ImageView.java:827) at android.widget.ImageView.setImageResource(ImageView.java:434) at application.example.com.fragment_practice.ImageAdapter.getView(ImageAdapter.java:48) at android.widget.AbsListView.obtainView(AbsListView.java:2937) at android.widget.GridView.makeAndAddView(GridView.java:1463) at android.widget.GridView.makeRow(GridView.java:366) at android.widget.GridView.fillUp(GridView.java:407) at android.widget.GridView.fillGap(GridView.java:282) at android.widget.AbsListView.trackMotionScroll(AbsListView.java:7307) at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:4400) at android.widget.AbsListView.onTouchMove(AbsListView.java:5796) at android.widget.AbsListView.onTouchEvent(AbsListView.java:5624) at android.view.View.dispatchTouchEvent(View.java:10011) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2833) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2504) at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:5561) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2840) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1853) at android.app.Activity.dispatchTouchEvent(Activity.java:3061) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63) at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2801) at android.view.View.dispatchPointerEvent(View.java:10246) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5447) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5283) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4721) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.
EDIT 1 : Anyone please explain why I am getting this issue on scrolling 3-4 time up and down grid view not on loading ?
Upvotes: 0
Views: 611
Reputation: 4023
I suggest you to use a library called Glide, to use this library add the followings in your gradle file
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
}
And then create a folder named drawable-nodpi
inside your res folder . Put all of your images there and don't forget to remove your images from drawable-hdpi
and other drawable folder . Make sure images only exist in drawable-nodpi
folder. Then instead of imageView.setImageResource(mThumbIds[position]);
this , do the following
Glide.with(mContext)
.load(mThumbIds[position])
.diskCacheStrategy( DiskCacheStrategy.NONE )
.skipMemoryCache( true )
.into(imageView);
Let me know if it works .
Upvotes: 0
Reputation: 417
Use Glide to load images:-
Add dependency in your gradle
compile 'com.github.bumptech.glide:glide:3.7.0'
Load Images:-
Glide.with(mContext).load(mThumbIds[position]).placeholder(R.drawable.user_placeholder).into(imageView);
If this will not work try this, Add in your app's build.gradle inside android block
dexOptions {
incremental true
javaMaxHeapSize "4g"
jumboMode = true
}
Upvotes: 3