Make image slider from url in android studio - java

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

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

Update Viewpager data dynamically

I have an ArrayList of my custom model class which I am instantiating by URL of images taken from Internet in my Home_Fragment using an AsyncTask
The first time the List is created it has 12 items/URL's from the website.
Now, I am passing these on a FullscreenView Activity that is linked to my ViewPager Adapter
-> I have set an onLoadMoreListener in my Home_Fragment that adds more items to the ArrayList, now I want to add those to my ViewPager Adapter dynamically,
How can I accomplish this?
Home_Fragment
class ReadHTML extends AsyncTask<String, Integer, String> {
List list;
#Override
protected String doInBackground(String... params) {
try {
Document document = Jsoup.connect(params[0]).get();
Element wall = document.select("ul.postul").first();
//Log.i("LIST ", wall.toString());
Elements url = wall.getElementsByAttribute("src");
list = url.eachAttr("src");
for (int i = 0; i < list.size(); i++) {
wallpaperNumber++;
String string = //Dummy
String septemp[] = //Dummy2
sep[1] = "http://" + string
wallpapersModelArrayList.add(new WallpapersModel(
string,///
sep[1],
"jpg",
wallpaperNumber
));
}
}catch (Exception e){}
return null;
}
Fullscreen Activity
setContentView(R.layout.fullscreen_activity_view);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = findViewById(R.id.view_pager);
arrayList = getIntent().getParcelableArrayListExtra("array");
final int pos = getIntent().getIntExtra("position", 0);
fullScreenSwipeAdapter = new
FullScreenSwipeAdapter(FullWallpaperViewActivity.this, arrayList);
viewPager.setAdapter(fullScreenSwipeAdapter);
viewPager.setCurrentItem(pos);
viewPager.setOnTouchListener(new View.OnTouchListener(){}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {}
ViewPager Adapter
public FullScreenSwipeAdapter(Activity activity, ArrayList<WallpapersModel> arrayList) {
this.activity = activity;
this.arrayList = arrayList;
Fresco.initialize(activity);
}
#Override
public int getCount() {
return this.arrayList.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.fullwallpaper_view_layout, container, false);
draweeView = v.findViewById(R.id.full_image_view);
draweeView.setImageURI(arrayList.get(position).getWallpaperFullURL());
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((CoordinatorLayout)object);
}
First make a new instance of your ArrayAdapter and set all the new items like this:
public class Activity() extends AppCompatActivity {
CustomArrayAdapter mArrayAdapter;
ViewPager mViewPager;
public void updateViewpager(){
mArrayAdapter = new ArrayAdapter(context, itemList); // old items with new items
mViewPager.setAdapter(mArrayAdapter);
mArrayAdapter.notifyDataSetChanged();
}
...
Hope it helps,
After adding new items to ArrayList, just notify your adapter of the changes using -
arrayAdapter.notifyDataSetChanged();

java.file.io is not getting resolved out

I've been working on making a video gallery for myself and got stuck here. Followed this link for some references but still getting some problems:
For making thumbnails for the videos
Here is my code :
public class AddFragment extends Fragment {
private ImageButton imageButton;
private GridView gridView;
private File files;
ArrayList<File> list;
public AddFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add, container, false);
imageButton = (ImageButton) view.findViewById(R.id.gotoButton);
gridView = (GridView) view.findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(getContext()));
list = videoReader(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//for making the button visible as ssonas the item gets selected
imageButton.setVisibility(view.VISIBLE);
}
});
return view;
}
ArrayList<File> videoReader(File root) {
ArrayList<File> arrayList = new ArrayList<>();
File[] file = root.listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isDirectory()){
}else{
if(file[i].getName().endsWith(".mp4")){
arrayList.add(file[i]);
}
}
}
return arrayList;
}
public class ImageAdapter extends BaseAdapter{
private Bitmap bitmap;
private final Context context;
private ImageAdapter(Context c){
context = c;
}
//for the video numbers
#Override
public int getCount() {
return list.size();
}
//for getting the video items position vise
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
if(list.get(position).contains(".jpg"))
{
bitmap = BitmapFactory.decodeFile(list.get(position)); //Creation of Thumbnail of image
}
else if(list.get(position).contains(".mp4"))
{
bitmap = ThumbnailUtils.createVideoThumbnail(list.get(position), 0); //Creation of Thumbnail of video
}
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
}
else
{
picturesView = (ImageView)convertView;
}
return picturesView;
}
} }
The problems I'm getting are in my ImageAdapter class in getView method
These are :
1. In the if(list.get(position).contains(".jpg")) //cannot resolve contains
2. In bitmap = BitmapFactory.decodeFile(list.get(position)); //saying the decodeFile(java.lang.string) from Bitmapfacotory cannot be applied to (java.file.io)
P.S. for the second option I tried doing that after getting reference from this link but failed:
Java contradicting behavior resolved
Try this.
if(list.get(position).contains(".jpg"))
{
bitmap = BitmapFactory.decodeFile(list.get(position).toString());
}
list.get(position) is File object and you need to pass String object so just make it String by writing .toString().
contains expect to see the same object type as the list items. if you want to check if the file is an image read this: Android: How to check if file is image?
Decode file expects to get a file url and not a file object. see here how to use it: Bitmapfactory example

How to set data in viewpager using ArrayList?

Here, in this viewpageradapter class is using to set data, how can I set data in viewpager. I can try to implement but I don't know how can be achieving this.I also add my PhotoviewAdater and PhotoActivity. plz solve this problem.
public class PhotoViewAdapter extends PagerAdapter {
Context context;
ArrayList<Model_image> all_folder = new ArrayList<>();
int int_position;
LayoutInflater inflater;
public PhotoViewAdapter(Context context, ArrayList<Model_image> all_folder, int int_position) {
this.context=context;
this.all_folder=all_folder;
this.int_position=int_position;
}
#Override
public int getCount() {
return all_folder.get(int_position).getAll_imagepath().size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.activity_photo_view_adapter, container,false);
imageView = (ImageView) container.findViewById(R.id.im_page);
try {
imageView.setImageResource(Integer.parseInt((all_folder.get(position).getAll_imagepath().get(int_position))));
} catch (Exception e) {
e.printStackTrace();
}
return viewLayout;
}
}
PhotoActivity:
public class PhotoActivity extends AppCompatActivity {
ViewPager viewPager;
int int_position;
PhotoViewAdapter photoViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_view);
int_position = getIntent().getIntExtra("pos",0);
viewPager = (ViewPager) findViewById(R.id.imagepage);
photoViewAdapter = new PhotoViewAdapter(getApplicationContext(), ImageActivity.all_images, int_position);
viewPager.setAdapter(photoViewAdapter);
}
}
Model_image:
public class Model_image {
String str_folder;
ArrayList<String> all_imagepath;
public void setStr_folder(String str_folder)
{
this.str_folder = str_folder;
}
public void setAll_imagepath(ArrayList<String> all_imagepath)
{
this.all_imagepath = all_imagepath;
}
public String getStr_folder()
{
return str_folder;
}
public ArrayList<String> getAll_imagepath()
{
return all_imagepath;
}
}
Change this
imageView.setImageResource(all_folder[position]);
to
imageView.setImageResource(all_folder.get(position));
all_folder is defined as an Arraylist not as an array.
all_folder.get(position) returns an Model_image object.
To set the image in the imageView you need an image reference.
I dont know which image you are trying to set.
If you want to set the image from all_imagepath, try doing
imageView.setImageResource(all_folder.get(position).getAll_imagepath.get(<position of the required image>));
setImageResource is used to set an image from the apk resources.
rather use imageView.setImageBitmap(BitmapFactory.decodeFile(all_folder.get(position).getPath()));
It is unclear yet from your Model_image class what an unique path can be, so you need to define a getPath() method.

android - viewpager OutOfMemoryError

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
...

Categories