android - viewpager OutOfMemoryError - java

I have a problem. I want to get image URL from JSON and setting to viewpager. I tried it with drawable folder, but large images are causing problems. If the image would be small, then I would not have a problem. How can I get large images? I benefited from this article : enter link description here
GalleryActivity
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
ViewPager viewPager = (ViewPager)findViewById(R.id.viewpa);
GalleryAdapter ga = new GalleryAdapter(this);
viewPager.setAdapter(ga);
}
Gallery Adapter (extending PagerAdapter)
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
GalleryAdapter(Context context){
this.context=context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);// error on this line when large image
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
And logcat outputs
java.lang.OutOfMemoryError at android.graphics.BitmapFactory
...

Related

How to pass dynamic image links in an image slider getting from server using rest api in android

I am creating an auto slider in android and it is working with drawable images i want to add images getting from server using rest api how can i do that please help here is my code. this is my adapter where i am passing drawable images to the slider...
public class HomeImageAdapter extends PagerAdapter {
private Context mContext;
private int[] mImageIds = new int[]{R.drawable.slideone , R.drawable.slideone , R.drawable.slideone};
public HomeImageAdapter(Context context){
mContext = context;
}
#Override
public int getCount() {
return mImageIds.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object o) {
return view == o;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mImageIds[position]);
container.addView(imageView , 0);
return imageView;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((ImageView) object);
}
}
In the above code i want to pass array of images getting from server please help

image viewpager onclicklistener issue

Used ViewPager for images sliding, when clicking on each image must do an action, its different action from image to another,
I'm new to android development
so I try to add onClick listener to ViewPager, but I can't achieve it,
please any suggestion how to implement OnClickListener so when I click the first image will do action and when click the second image will do another action and so on for all images, I have it inside the view pager
thanks in advance.
MainActivity:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
}
}
ImageAdapter:
public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.g,
R.drawable.h,
};
ImageAdapter(Context context){
this.context=context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
intpadding=context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
To catch click events in ViewPager you have to override onTouchListener, since default implementation is not made to handle clicks
Your MainActivity should look something like this:
private ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager= (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
mViewPager.setAdapter(adapter);
final GestureDetector tapGestureDetector = new GestureDetector(this, new TapGestureListener());
mViewPager.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
tapGestureDetector.onTouchEvent(event);
return false;
}
});
}
private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if (mViewPager.getCurrentItem() == 0) {
//do your thing tied with first page
} else if (mViewPager.getCurrentItem() == 1) {
//do your thing tied with second page
} else if (mViewPager.getCurrentItem() == 2) {
//do your thing tied with third page
}
return super.onSingleTapConfirmed(e);
}
}
Try this:
#Override
public Object instantiateItem(View collection, final int pos) { //have
to make final so we can see it inside of onClick()
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View page = inflater.inflate(R.layout.YOUR_PAGE, null);
page.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//this will log the page number that was click do your action
according to position
Log.i("TAG", "This page was clicked: " + pos);
}
});
((ViewPager) collection).addView(page, 0);
return page;
}

Make image slider from url in android studio

I'm beginner in android, i have image slider but image inside in source project, how can get image from url and show in image slider.
This is my adapter :
public class adapter_image_slider extends PagerAdapter {
Context mContext;
public adapter_image_slider(Context context) {
this.mContext = context;
}
#Override
public int getCount() {
return sliderImagesId.length;
}
private int[] sliderImagesId = new int[]{
R.drawable.imghome1, R.drawable.imghome2, R.drawable.imghome3,
};
#Override
public boolean isViewFromObject(View v, Object obj) {
return v == ((ImageView) obj);
}
#Override
public Object instantiateItem(ViewGroup container, int i) {
ImageView mImageView = new ImageView(mContext);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mImageView.setImageResource(sliderImagesId[i]);
((ViewPager) container).addView(mImageView, 0);
return mImageView;
}
#Override
public void destroyItem(ViewGroup container, int i, Object obj) {
((ViewPager) container).removeView((ImageView) obj);
}
}
and this my main_activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_slider);
ViewPager mViewPager = (ViewPager) findViewById(R.id.viewPageAndroid);
adapter_image_slider adapterView = new adapter_image_slider(this);
mViewPager.setAdapter(adapterView);
}
Somebody please help...
You can use Glide or Picasso.
https://github.com/bumptech/glide
Glide is a fast and efficient open source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.
https://github.com/square/picasso
Picasso: A powerful image downloading and caching library for Android
you can create constructor with ArrayList ids ....
public class ImagePagerAdapter extends PagerAdapter{
ArrayList<Integer> imageIds;
public ImagePagerAdapter(ArrayList<Integer> imageIds) {
this.imageIds = imageIds;
}
and send ids and use it.
ArrayList<Integer> imageIds = new ArrayList<Integer>();
for(int i = 1 ; i < 15 ; i++) {
int id = getApplicationContext.getResources().getIdentifier("image" + i,"drawable" ,getApplicationContext.getPackageName() );
imageIds.add(id);
}
But it does not use the URL. just reads from storage

how to setup onClickListeners for each slide in a viewpager?

The ImageAdapter Class
public class ImageAdapter extends PagerAdapter implements AdapterView.OnItemClickListener {
Context context;
private int[] GalImages = new int[] {//array storing all images used in the 'slideshow' in the main menu
R.drawable.savedpageex,
R.drawable.ic_cdi_icon,
R.drawable.savedpageex,
R.drawable.ic_cdi_icon
};
ImageAdapter(Context context){
this.context=context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
// int padding = context.getResources().getDimensionPixelSize(R.dimen.abc_panel_menu_list_width);
imageView.setPadding(1, 1, 1, 1);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final int clickedImage = GalImages[position];
}
}
In the MainActivity
ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
ImageAdapter adapter = new ImageAdapter(this);
wrappedAdapter = new InfinitePagerAdapter(adapter);
viewPager.setAdapter(wrappedAdapter);
viewPager.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show();
}
});
Right now, the pictures show up and they loop, I do need help getting the pictures to 'slide' at set time intervals, but the main issue is with setting up onClickListeners for each 'page'. Any help/advice?
You could add a switch statement inside of the onClickListener like this
switch(viewPager.getCurrentItem()){
case 0:
//stuff for first page
break;
case 1:
//stuff for second page
break;
}
...... etc.
or add a state to the viewpager to describe which "page" it is showing
and using that information do different things in the onClickListener

ImageView will not resize properly

I'm attempting to get an imageview (classical_up_btn) to fill the width of the screen correctly however it appears to be cut off by the next image it is supposed to load when swiped
Any suggestions as to how this can be resolved?
Thanks!
SCREENSHOT:
JAVA:
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] { R.drawable.classical_up_btn,
R.drawable.country_up_btn, R.drawable.dance_up_btn,
R.drawable.hiphop_up_btn };
public int getCount() {
return mImages.length;
}
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
public Object instantiateItem(ViewGroup container, int position) {
Context context = Home.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
imageView.setScaleType(ImageView.ScaleType.CENTER);
should be:
imageView.setScaleType(ImageView.ScaleType.FIT_XY);

Categories