Movie Poster Not Displaying w/ TheMovieDB - java

I'm not exactly sure what I'm missing, but I've been trying to get the most popular movie posters to display in a gridview from themoviedb API utilizing the Picasso library. Anyone know what I'm doing wrong?
Here's my ImageAdapter:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 20;
}
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) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(mContext).load("https://api.themoviedb.org/3/movie/popular?api_key={APIKEY_HERE}&poster_path&images").into(imageView);
} else {
imageView = (ImageView) convertView;
}
return imageView;
}
}

Above mentioned API will give popular movie list this..
results: [
{
poster_path: "/inVq3FRqcYIRl2la8iZikYYxFNR.jpg",
adult: false,
overview: "life.",
release_date: "2016-02-09",
backdrop_path: "/nbIrDhOtUpdD9HKDBRy02a8VhpV.jpg",
popularity: 91.92864,
vote_count: 3497,
video: false,
vote_average: 7.2
}
]
Extract poster_path of which movie image you want to display
Use the below URL to display image http://image.tmdb.org/t/p/w500/inVq3FRqcYIRl2la8iZikYYxFNR.jpg
You can find the document here http://docs.themoviedb.apiary.io/#reference/configuration/configuration

you have to check that url in picasso it returns the whole data not the image url, try to add the value of poster_path to the right url

Related

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

int array of Images implementing Picasso

This is my code
public class ImageAdapter extends BaseAdapter {
ImageView imageView;
private Context context;
public int Images[] = {Integer.parseInt(("http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg")),
Integer.parseInt("https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300"),
Integer.parseInt("https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg"),
Integer.parseInt("http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg")};
public ImageAdapter (Context c){
context= c;
}
#Override
public int getCount() {
return Images.length;
}
#Override
public Object getItem(int position) {
return Images[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview = new ImageView(context);
imageview.setImageResource(Images[position]);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setLayoutParams(new GridView.LayoutParams(240,240));
Picasso.with(context).load(Images[position]).into(imageview);
return imageview;
}
}
I am trying to implement Picasso in my project the problem i am facing is
I have an int array of Images .and i am getting error java.lang.NumberFormatException ,i guess its because there is no pars Int in array of Images.And if i make it String i.e
public String Images[] = {("URL"),("URL"),("URL"),("URL"),("URL")};
But setimageResourcewants an int value please help me i am stuck with this .Any help would really be appreciated .
Thanks.
Syntax from the picasso site:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Use a String[] instead of a Integer[] would be the correct solution
public String[] Images = { "http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg",
"https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300",
"https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg",
"http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg" };
You don't need imageview.setImageResource(Images[position]);when you load the image with Picasso, so just remove that line

How to append an ArrayList of ImageViews into a gridView

I am new to android and I'm trying to figure out how to create a gallery of ImageViews on an app.
I have a server running that serves the images to the app which download correctly into a Arraylist of ImageViews.
I just need to add all of these images to a grid view.
I know I need to use an adaptor but I don't know how.
This is the code I have in my activity (images is populated else where)
ArrayList<ImageView> images = new ArrayList<>();
GridView table = (GridView) findViewById(R.id.table);
table.setAdapter(new ImageAdaptor(this.getBaseContext(), images));
And this is my adaptor
public class ImageAdaptor extends BaseAdapter{
private Context context;
private ArrayList<ImageView> images;
public ImageAdaptor(Context context, ArrayList<ImageView> images){
this.context = context;
this.images = images;
}
public int getCount(){
return this.images.size();
}
public Object getItem(int position){
return this.images.get(position);
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView image;
if (convertView == null){
image = new ImageView(this.context);
image.setLayoutParams(new GridView.LayoutParams(115, 115));
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
}else{
image = (ImageView) convertView;
}
return image;
}
}
First thing, you should not make the arraylist of views as it wont be efficient for reusing views. However if you still want to do that, then solution is ,
public View getView(int position, View convertView, ViewGroup parent){
return images.get(position);
}

ListView items reordering and repeating using baseadapter

I am trying to implement a listview that displays a list of directories. and under each directory is a gridview with associated adapter (shown below) showing a list of image thumbnails (see below image). I have it working great except whenever the list item is off the screen then brought back on screen, the images are reloaded. I am using an asynctask to download the thumbnails and replace the placeholder image for each imageview so it is not acceptable that everytime an item is offscreen, all of its thumbnails are downloaded again. Does anyone have an example of this type of implementation (gridview adapter within a listview adapter) where the imageview (or images) are stored? What is the proper way to do this?
Thanks in advance for your help.
Gallery Adapter
public class GalleryAdapter extends BaseAdapter {
private Context mContext;
ArrayList<GalleryItem> GalleryList;
//MediaAdapter adapter;
public GalleryAdapter(Context c,ArrayList<GalleryItem> l) {
mContext = c;
GalleryList = l;
}
public int getCount() {
return GalleryList.size();
}
public Object getItem(int position) {
return GalleryList.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
GalleryViewHolder viewHolder = null;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.gallery_item, parent, false);
viewHolder = new GalleryViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.folder_settings = (ImageView) convertView.findViewById(R.id.folder_settings);
viewHolder.mediaGrid = (GridView) convertView.findViewById(R.id.imagegrid);
viewHolder.gridHolder = (LinearLayout) convertView.findViewById(R.id.gridholder);
convertView.setTag(viewHolder);
}
else{
viewHolder = (GalleryViewHolder) convertView.getTag();
}
viewHolder.title.setText(GalleryList.get(position).getTitle());
//Formatting the gridView to fit the screen dim.
ImageTools mWidth = new ImageTools(mContext);
viewHolder.mediaGrid.setColumnWidth(mWidth.imageSize());
int rows = (int) Math.ceil((GalleryList.get(position).getMedia().size() / mWidth.columnNumber)+1);
LinearLayout.LayoutParams labelLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, mWidth.imageSize()*rows);
viewHolder.gridHolder.setLayoutParams(labelLayoutParams);
viewHolder.mediaGrid.setLayoutParams(labelLayoutParams);
viewHolder.mediaGrid.setMinimumHeight(mWidth.imageSize()*rows);
//Set Adapter for image views
viewHolder.mediaGrid.setAdapter(new MediaAdapter(convertView.getContext(),GalleryList.get(position).getMedia()));
viewHolder.folder_settings.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Dialogs.createListDialog(mContext,"Folder Actions", R.array.gallery_action_array).show();
}
});
viewHolder.mediaGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
}
});
return convertView;
}
private class GalleryViewHolder {
private TextView title;
private ArrayList<ImageView> imageList;
private GridView mediaGrid;
private ImageView folder_settings;
private LinearLayout gridHolder;
private int position;
}
}
Media Adapter
public class MediaAdapter extends BaseAdapter {
private Context mContext;
ArrayList<MediaItem> mediaitems;
public MediaAdapter(Context c,ArrayList<MediaItem> l) {
mContext = c;
mediaitems = l;
}
public int getCount() {
return mediaitems.size();
}
public Object getItem(int position) {
return mediaitems.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setImageResource(R.drawable.loading);
imageView.setTag(R.integer.path,mediaitems.get(position).getPath().toString());
imageView.setTag(R.integer.fullsize,"false");
imageView.setTag(R.integer.parentpath,mediaitems.get(position).getParentPath().toString());
imageView.setTag(R.integer.index , String.valueOf(position));
try {
new thumbDownload(mContext).execute(imageView);
} catch (DbxException e) {
e.printStackTrace();
}
ImageTools mWidth = new ImageTools(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(mWidth.imageSize(), mWidth.imageSize()));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
//imageView.setImageBitmap(mediaitems.get(position).getBitmap());
return imageView;
}
}
Try with this..may be its silly way, but its worked for me. Just add a line of code inside your method like this for gallery adapter:
public View getView(int position, View convertView, ViewGroup parent) {
GalleryViewHolder viewHolder = null;
// Add this line.
convertView = null;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.gallery_item, parent, false);
viewHolder = new GalleryViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.folder_settings = (ImageView) convertView.findViewById(R.id.folder_settings);
viewHolder.mediaGrid = (GridView) convertView.findViewById(R.id.imagegrid);
viewHolder.gridHolder = (LinearLayout) convertView.findViewById(R.id.gridholder);
convertView.setTag(viewHolder);
}
else{
viewHolder = (GalleryViewHolder) convertView.getTag();
}
// rest of your code
}
You can use StickyGridHeaders to implement your UI and Android-Universal-Image-Loader for flexible asynchronous image loading.
StickyGridHeaders is an Android library that provides a GridView that shows items in sections with headers. By default the section headers stick to the top like the People app in Android 4.x but this can be turned off.
Android-Universal-Image-Loader aims to provide a reusable instrument for asynchronous image loading, caching and displaying.
I ended up Using a HashMap<String,Bitmap> to store the images once they were downloaded. I made the hashMap static in my mediaAdapter so I could add the bitmap from my asynctask when it was downloaded. Then in my media Adapter getView(), I added a if statement to check if the image had already been downloaded. If it had, I used setImageBitmap(myHash.get(key)).

using gridview with images in android

this is the problematic code:
public class Level1 extends Activity {
int[] logos = {
R.drawable.arutz8,
R.drawable.channel1,
R.drawable.doctor_gav,
R.drawable.foxgroup3,
R.drawable.careline,
R.drawable.golfnew,
R.drawable.haaretz,
R.drawable.hafenix,
/*R.drawable.hando,
R.drawable.bankleumi,
R.drawable.jerusalempostred,
R.drawable.laisha,
R.drawable.logo,
R.drawable.logodelta,
R.drawable.maariv,
R.drawable.pelephone,
R.drawable.ravbariah,
R.drawable.renuar,
R.drawable.reshet_tv,
R.drawable.sano,
R.drawable.shilav,
R.drawable.sport5,
R.drawable.srigamish,
R.drawable.steimatzky,
R.drawable.superpharm,
R.drawable.supersal,
R.drawable.tambur,
R.drawable.tzometsfarim,
R.drawable.walla,
R.drawable.yediot,*/
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.level1);
ListAdapter adapter = (new ArrayAdapter<Integer>(this, R.layout.level1));
GridView grid = (GridView) findViewById(R.id.gridview1);
grid.setAdapter(new ImageAdapter(this));
}
private class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageAdapter(Context c)
{
context = c;
}
//---returns the number of images---
public int getCount() {
return logos.length;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(90, 90));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(logos[position]);
return imageView;
}
}
}
This program works perfectly as long as the size of all images is about 60px per each image. The problem in that size of image is that every image got constricted and its ugli!.
I tried to use in bigger images and for some reason only the first five images appears on the screen and when i try to load the rest images, the app crashes.
I thought that happens because of the size of the images but than i realized that all the images are in the same big size and still 5 of them were drawn on the screen.
Any ideas?
Those are the logs from logCat:
logs
and this is the result when i press on the level 1 button:
result
There is a problem in your public View getView(...) method in your ImageAdapter.
When the convertView == null, you never link the convertView to your imageView. So there will be a problem in the else-statement.
You can do this in two ways:
By defining your ImageView in code like you did, in your case this will be the best choice:
ImageView imageView = (ImageView) convertView;
if (convertView == null) {
convertView = new ImageView(context);
imageView = (ImageView) convertView;
// Set other parameters
}
// Set resource
return convertView;
By defining your layout in a xml-layout file an using a layout inflater:
Handler handler;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(yourLayoutResourceId, parent, false);
handler = new Handler();
handler.itemIV = (ImageView) convertView.findViewById(imageViewLayoutId);
convertView.setTag(handler);
} else {
handler = (Handler) convertView.getTag();
}
handler.imageView.setImageResource(...);
return convertView;
Where yourLayoutResourceId is the id of the created xml-layout file (R.layout.exmaple), and imageViewLayoutId is the id of your imaeView in the layout (R.id.exmapleIV).
As last step define an inner class Handler in your ImageAdapter:
class Handler {
ImageView imageView;
}
Good luck!
Have nice development.
Kr

Categories