My code is showing an error and I don't know what is actually causing it, this line: HttpURLConnection con = client.open(new URL(imageUrl));
package www.com.easyrecepee;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.util.LruCache;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
/**
* Created by Joshua on 9/28/2016.
*/
public class PlacesListAdapter extends ArrayAdapter<Places> {
private Context context;
private List<Places> placesList;
private LruCache<String, Bitmap> imageCache;
// create a field of the RequestQueue to be used by volley
// so it persist through out the life time of the class
//private RequestQueue queue;
public OkHttpClient client = new OkHttpClient();
public PlacesListAdapter(Context context, int resources, List<Places> objects) {
super(context, resources, objects);
this.context = context;
this.placesList = objects;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
imageCache = new LruCache<>(cacheSize);
// instantiate the queue field and passing current context
// so its associated with proper activity
//queue = Volley.newRequestQueue(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_vicinity, parent, false);
// Display places name in the TextView widget
final Places places = placesList.get(position);
TextView placeTitle = (TextView) view.findViewById(R.id.place_title);
TextView vicinity = (TextView) view.findViewById(R.id.vicinity);
//TextView openNow = (TextView)view.findViewById(R.id.openNow);
placeTitle.setText(places.getName());
vicinity.setText(places.getVicinity());
// if(places.isOpenNow()){
// openNow.setText("Open");
// } else {
// openNow.setText("Closed");
// }
//Display place photo in ImageView widget
Bitmap bitmap = imageCache.get(places.getId());
// for volley
final ImageView image = (ImageView) view.findViewById(R.id.place_image);
if (bitmap != null) {
//For the volley, this commented line of code is refactored so it can
// be called outside this block
//ImageView image = (ImageView) view.findViewById(R.id.place_image);
image.setImageBitmap(places.getBitmap());
} else {
// Retrieves image url
//String imageUrl = places.getIconUrl();
// declare instance of image request
/*ImageRequest request = new ImageRequest(imageUrl,
new Response.Listener<Bitmap>(){
#Override
public void onResponse(Bitmap arg0) {
image.setImageBitmap(arg0);
imageCache.put(places.getId(), arg0);
}
},
80, 80,
Bitmap.Config.ARGB_8888,
new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError arg0) {
Log.d("PlacesAdapter", arg0.getMessage());
}
}
);
//adding request to queue
queue.add(request);*/
/*
The line of code below is used by the ImageLoader AsyncTask
Uncomment if using AsyncTask
*/
PlaceAndView container = new PlaceAndView();
container.places = places;
container.view = view;
ImageLoader loader = new ImageLoader();
loader.execute(container);
}
return view;
}
// Used with AsyncTask, since am using Volley, no need
class PlaceAndView {
public Places places;
public View view;
public Bitmap bitmap;
}
// Using the Volley Method does what the AsyncTask do
// Uncomment to use
private class ImageLoader extends AsyncTask<PlaceAndView, Void, PlaceAndView> {
#Override
protected PlaceAndView doInBackground(PlaceAndView... params) {
PlaceAndView container = params[0];
Places places = container.places;
try {
String imageUrl = places.getIconUrl();
// Using OkHttpClient to fetch images
HttpURLConnection con = client.open(new URL(imageUrl));
//HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = con.getInputStream();
}
//InputStream in = (InputStream) new URL(imageUrl).getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
places.setBitmap(bitmap);
in.close();
container.bitmap = bitmap;
return container;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(PlaceAndView result) {
ImageView image = (ImageView) result.view.findViewById(R.id.place_image);
image.setImageBitmap(result.bitmap);
// saves image for future use
//result.places.setBitmap(result.bitmap);
imageCache.put(result.places.getId(), result.bitmap);
}
}
}
How do I make the open() method stop showing error and that's the only method creating error in the code, so I feel there should be some other method or a newer way to handle the open method.
I suppose you copy the same code in here Fatal Exception: Exception in do in background method (AsyncTask)
So i guess you can adapt this code as need and see if your code runs.
#Override
protected FlowerAndView doInBackground(FlowerAndView... params) {
FlowerAndView container = params[0];
Flower flower = container.flower;
try {
String imageUrl = MainActivity.PHOTOS_BASE_URL + flower.getPhoto();
InputStream in = (InputStream) new URL(imageUrl).getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
flower.setBitmap(bitmap);
in.close();
container.bitmap = bitmap;
return container;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
[EDITED]
But using OkHttpClient your code should be something like this.
#Override
protected PlaceAndView doInBackground(PlaceAndView... params) {
PlaceAndView container = params[0];
Places places = container.places;
try {
String imageUrl = places.getIconUrl();
// Using OkHttpClient to fetch images
// HttpURLConnection con = client.open(new URL(imageUrl));
//HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//InputStream in = con.getInputStream();
Request req = new Request.Build()
.url(imageUrl)
.build();
Response response = client.newCall(req).execute();
InputStream in = response.body().byteStream();
//InputStream in = (InputStream) new URL(imageUrl).getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
places.setBitmap(bitmap);
in.close();
container.bitmap = bitmap;
return container;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
PS: Remember, there are a lot of libraries to download images glide, picasso, etc. you save time and code.
An example how to use Glide, note how less code you need.
public class PlacesListAdapter extends ArrayAdapter<Places> {
private LayoutInflater mInflater;
public PlacesListAdapter(Context context, int resources, List<Places> objects) {
super(context, resources, objects);
mInflater = LayoutInflater.from(context);
}
class Holder {
TextView placeTitle;
TextView vicinity;
ImageView placeImage;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Holder holder;
if (view == null) {
view = mInflater.inflate(R.layout.item_vicinity, parent, false);
holder = new Holder();
holder.placeTitle = (TextView) view.findViewById(R.id.place_title);
holder.vicinity (TextView) view.findViewById(R.id.vicinity);
holder.placeImage = (ImageView) view.findViewById(R.id.place_image);
view.setTag(holder);
} else {
holder = view.getTag();
}
// Display places name in the TextView widget
final Places places = getItem(position);
holder.placeTitle.setText(places.getName());
holder.vicinity.setText(places.getVicinity());
Glide
.with(parent.getContext())
.load(places.getIconUrl())
.into(holder.placeImage);
return view;
}
}
Related
This app is supposed to display movie's tittles fetched from an API into a RecyclerView. I know I am getting the data right because of logs I made on the movies Arraylist, but for some reason, I can't display the info. The app does not crash, it just shows an empty view. I do really appreciate your help.
Disclaimers: I am a newbie, and this solution might be quite simple for experienced people. I usually don't ask questions unless I can't solve the issue, actually this is my first question ever. I've been around this for a few days now and I can't find a way. I understand that this code might have more than one problem on it, and you are very welcome to point them all out.
SO, this is my main activity
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public static final String LOG_TAG = MainActivity.class.getSimpleName();
private static final String MOST_POPULAR_MOVIES = "http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=XXXXXXXXXXXXXXXXXXX";
ArrayList<Pelicula> ArrayDePeliculas;
private RecyclerView mPelisList;
private AdapterRV adapterRV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PeliculasAsyncTask task = new PeliculasAsyncTask();
task.execute(createUrl(MOST_POPULAR_MOVIES));
adapterRV = new AdapterRV(getApplicationContext(),/*Aca puede faltar algo*/ new ArrayList<Pelicula>());
mPelisList = findViewById(R.id.rv_pelis);
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
mPelisList.setLayoutManager(layoutManager);
mPelisList.setAdapter(adapterRV);
}
public void updateUi(ArrayList<Pelicula> peliculas) {
}
/**
* Returns new URL object from the given string URL.
*/
private URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException exception) {
Log.e(LOG_TAG, "Error with creating URL", exception);
return null;
}
return url;
}
/**
* Make an HTTP request to the given URL and return a String as the response.
*/
private String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
// If the URL is null, then return early.
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.connect();
// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
// function must handle java.io.IOException here
inputStream.close();
}
}
return jsonResponse;
}
/**
* Convert the {#link InputStream} into a String which contains the
* whole JSON response from the server.
*/
private String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
/**
* TODO modificar la url que le estas pasando
*/
public ArrayList<Pelicula> extractFeatureFromJson(String jsonResponse) {
ArrayList<Pelicula> arraydePelis = new ArrayList<>();
try {
JSONObject baseJsonResponse = new JSONObject(jsonResponse);
JSONArray resultsArray = baseJsonResponse.getJSONArray("results");
for (int i = 0; i < resultsArray.length(); i++) {
// If there are results in the features array
if (resultsArray.length() > 0) {
// Extract out the first feature (which is an earthquake)
JSONObject firstResult = resultsArray.getJSONObject(i);
String tittle = firstResult.getString("title");
Pelicula pelicula = new Pelicula(tittle);
arraydePelis.add(pelicula);
}
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem parsing the earthquake JSON results", e);
}
return arraydePelis;
}
private class PeliculasAsyncTask extends AsyncTask<URL, Void, ArrayList<Pelicula>> {
#Override
protected ArrayList<Pelicula> doInBackground(URL... urls) {
// Create URL object
URL url = createUrl(MOST_POPULAR_MOVIES);
// Perform HTTP request to the URL and receive a JSON response back
String jsonResponse = "";
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
Log.e(LOG_TAG, "Problem making the HTTP request.", e);
}
// Extract relevant fields from the JSON response and create an {#link Event} object
ArrayList<Pelicula> ArrayPeliculas = extractFeatureFromJson(jsonResponse);
// Return the {#link Event} object as the result fo the {#link TsunamiAsyncTask}
return ArrayPeliculas;
}
#Override
protected void onPostExecute(ArrayList<Pelicula> data) {
if (data == null) {
return;
}
adapterRV.notifyDataSetChanged();
mPelisList.setAdapter(adapterRV);
Log.e("Nada", data.get(6).getTittle());
super.onPostExecute(data);
}
}
}
The Movie class
public class Pelicula {
//Fields
private String mTittle;
//Constructor
public Pelicula(String Tittle){
mTittle= Tittle;
}
//Methods
public String getTittle (){
return mTittle;
}
}
And finally, the recyclerView adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class AdapterRV extends RecyclerView.Adapter<AdapterRV.PelisViewHolder> {
//Fields
List<Pelicula> mPeliculas;
private Context context;
/* TODO, comprobar que no necesitas esto: mNumberItems = numberOfItems; y poner autofit en grid view */
//Constructor
public AdapterRV (Context context, ArrayList<Pelicula> peliculas){
mPeliculas = peliculas;
this.context= context;
}
//Metodos
#NonNull
#Override
public PelisViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
int layoutForListItem = R.layout.item_layout;
LayoutInflater inflater= LayoutInflater.from(context);
View view = inflater.inflate(layoutForListItem, parent, false);
PelisViewHolder viewHolder = new PelisViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull PelisViewHolder holder, int position) {
Pelicula peliculas = mPeliculas.get(position);
TextView textView = holder.listItemTittleView;
textView.setText(peliculas.getTittle());
}
#Override
public int getItemCount() {
return mPeliculas.size();
}
public class PelisViewHolder extends RecyclerView.ViewHolder{
TextView listItemTittleView;
public PelisViewHolder(#NonNull View itemView) {
super(itemView);
listItemTittleView = itemView.findViewById(R.id.tv_title);
}
}
}
The problem is that you are passing new ArrayList<Pelicula>() to your adapter, and this list is remaining empty. I suggest you declare a global reference of Pelicula list inside your MainActivity and then pass it to the adapter. Whenever that list changes, then notifyDataSetChanged.
If you don't want to declare a global reference, just declare a method inside your adapter:
public void setList(ArrayList list){
this.list = list;
}
and in your MainActivity, just everything you get a new list, setList(newPeliculaList) and adapterRV.notifyDataSetChanged()
Edit:
in onCreate() before declaring the AdapterRV:
just put this:
ArrayDePeliculas = new ArrayList<Pelicula>();
Do you see these 2 lines in onCreate()
adapterRV = new AdapterRV(getApplicationContext(),new ArrayList<Pelicula>());
...
...
mPelisList.setAdapter(adapterRV);
Move them to onPostExecute() of the asyncTask and pass the data instead of a new ArrayList<Pelicula>():
#Override
protected void onPostExecute(ArrayList<Pelicula> data) {
//assuming the list is full and all is good
if (data == null) {
return;
}
//here...
adapterRV = new AdapterRV(getApplicationContext(),data);
mPelisList.setAdapter(adapterRV);
Log.e("Nada", data.get(6).getTittle());
super.onPostExecute(data);
}
UPDATE:
Its because you don't initialize the list in the adapter:
public class AdapterRV extends RecyclerView.Adapter<AdapterRV.PelisViewHolder> {
//Fields
//here you must initialize like this:
List<Pelicula> mPeliculas = new ArrayList<Pelicula>();
.....
I have been trying too hard to get around this problom, when I tap the refresh button in my app, the images doesn't load on the screen.
This the DataFragment file, when i build this app there are no errors.
package com.example.popularmovis;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.jar.JarException;
/**
* A placeholder fragment containing a simple view.
*/
public class DataFragment extends Fragment {
public ImageAdapter display;
public DataFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add this line in order for this fragment to handle menu events.
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.datafragment_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_refresh) {
FetchMovieData movieData = new FetchMovieData();
movieData.execute();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
display = new ImageAdapter(rootView.getContext());
GridView gridView = (GridView) rootView.findViewById(R.id.gridView_movies);
gridView.setAdapter(display); // uses the view to get the context instead of getActivity().
return rootView;
}
public class FetchMovieData extends AsyncTask<Void, Void, String[]>{
private final String LOG_TAG = FetchMovieData.class.getSimpleName();
private String[] extractdisplayFromJason(String jasnData)
throws JSONException{
//These jason display need to be extracted
final String movie_results = "results";
final String movie_poster = "poster_path";
JSONObject movieData = new JSONObject(jasnData);
JSONArray movieArray = movieData.getJSONArray(movie_results);
String[] posterAddreess = new String[movieArray.length()];
//Fill ther posterAddreess with the URL to display
for(int j=0; j < movieArray.length(); j++){
String posterPath;
JSONObject movieArrayItem = movieArray.getJSONObject(j);
posterPath = movieArrayItem.getString(movie_poster);
posterAddreess[j] = "http://image.tmdb.org/t/p/w185/" + posterPath;
}
for (String s : posterAddreess) {
Log.v(LOG_TAG, "Thumbnail Links: " + s);
}
return posterAddreess;
}
#Override
protected String[] doInBackground(Void... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String JsonData = null;
String country = "US";
String rating = "R";
String sort = "popularity.desc";
String apiKey = "";
//building the URL for the movie DB
try {
//building the URL for the movie DB
final String MovieDatabaseUrl = "https://api.themoviedb.org/3/discover/movie?";
final String Country_for_release = "certification_country";
final String Rating = "certification";
final String Sorting_Order = "sort_by";
final String Api_Id = "api_key";
Uri buildUri = Uri.parse(MovieDatabaseUrl).buildUpon()
.appendQueryParameter(Country_for_release, country)
.appendQueryParameter(Rating, rating)
.appendQueryParameter(Sorting_Order, sort)
.appendQueryParameter(Api_Id, apiKey)
.build();
//Converting URI to URL
URL url = new URL(buildUri.toString());
Log.v(LOG_TAG, "Built URI = "+ buildUri.toString());
//Create the request to the Movie Database
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
//Read the input from the database into string
InputStream inputStream = urlConnection.getInputStream();
//StringBuffer for string Manipulation
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
JsonData = buffer.toString();
Log.v(LOG_TAG,"Forecaast Jason String" + JsonData );
} catch (IOException e) {
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
try {
return extractdisplayFromJason(JsonData);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String[] posterAddreess) {
for (String poster : posterAddreess) {
display.addItem(poster);
}
}
}
}
This the ImageAdapter code
package com.example.popularmovis;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.example.popularmovis.R;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ArrayList<String> images = new ArrayList<String>();
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount(){
return images.size();
}
#Override
public Object getItem(int position){
return images.get(position);
}
public long getItemId(int position){
return 0;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView imageview;
if (convertView == null){
imageview = new ImageView(mContext);
imageview.setPadding(0, 0, 0, 0);
//imageview.setLayoutParams(new GridLayout.MarginLayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageview.setAdjustViewBounds(true);
} else {
imageview = (ImageView) convertView;
}
Picasso.with(mContext).load(images.get(position)).placeholder(R.mipmap.ic_launcher).into(imageview);
return imageview;
}
/*
Custom methods
*/
public void addItem(String url){
images.add(url);
}
public void clearItems() {
images.clear();
}
}
When you use this in your onCreate method like this, it means that the object is local to the method and can't be use in other methods.
ImageAdapter display = new ImageAdapter(rootView.getContext());
First create a global class member variable
private ImageAdapter display;
then inside your onCreate method do this
display = new ImageAdapter(rootView.getContext());
I am trying to create android app with image gallery created with grid view using image adapter which fetch data using picasso library.but no image is displayed.It may be a problem of context or in picasso library but i am not sure where is the problem as i get no error.everything works fine according to Logs
**package com.example.android.popmovies;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
/**
* A placeholder fragment containing a simple view.
*/
public class PopMovieFragment extends Fragment {
ImageAdapter imageAdapter;
public PopMovieFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main,container,false);
GridView gridView = (GridView) view.findViewById(R.id.gridview);
imageAdapter = new ImageAdapter(getActivity(),new ArrayList<String>());
gridView.setAdapter(imageAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("check","ok");
}
});
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onStart() {
super.onStart();
updateMovies();
}
public void updateMovies() {
FetchMovieTask movieTask = new FetchMovieTask();
movieTask.execute();
}
public class FetchMovieTask extends AsyncTask<String, Void, String[]> {
final String LOG_TAG = FetchMovieTask.class.getSimpleName();
private String[] getMovieImageFromJson(String movieJsonStr)
throws JSONException {
ArrayList<String> movieResults = new ArrayList<>();
String posterUrl;
final String OWM_MOVIE_RESULTS = "results";
JSONObject jsonObject = new JSONObject(movieJsonStr);
if (jsonObject.has(OWM_MOVIE_RESULTS)) {
JSONArray movieArray = jsonObject.getJSONArray(OWM_MOVIE_RESULTS);
//JSONArray movieArray = jsonObject.getJSONArray(OWM_MOVIE_RESULTS);
for (int i = 0; i < movieArray.length(); i++) {
JSONObject movieObject = movieArray.getJSONObject(i);
posterUrl = movieObject.getString("poster_path");
movieResults.add(posterUrl);
Log.v(LOG_TAG, posterUrl);
}
for (String items : movieResults){
Log.v(LOG_TAG,items);
}
// return null;
}
return movieResults.toArray(new String[0]);
// Log.v(LOG_TAG,posterUrl);
}
#Override
protected String[] doInBackground(String... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String movieJsonStr = null;
//parameters for api link will be here
try {
final String MOVIE_BASE_URL = "https://api.themoviedb.org/3/movie";
final String APPID_PARAM = "api_key";
final String CRITERIA_FOR_MOVIE_SELECTION_TOP = "top_rated";
final String CRITERIA_FOR_MOVIE_SELECTION_POPULAR = "popular";
Uri builtUri = Uri.parse(MOVIE_BASE_URL).buildUpon()
.appendPath(CRITERIA_FOR_MOVIE_SELECTION_TOP)
.appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_MOVIEDB_API_KEY)
.build();
Log.v("TAG","BuiltURI" + builtUri.toString());
URL url = new URL(builtUri.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read input stream into a String
InputStream inputstream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputstream == null) {
movieJsonStr = null;
}
reader = new BufferedReader(new InputStreamReader(inputstream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
movieJsonStr = null;
}
movieJsonStr = buffer.toString();
Log.v(LOG_TAG, "Movie Json String" + movieJsonStr);
} catch (IOException e) {
Log.e(LOG_TAG, "error here" + e);
movieJsonStr = null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "ErrorClosingStream", e);
}
}
}
try {
// here will be method call for JSon parsing.
String [] check_items = getMovieImageFromJson(movieJsonStr);
for (String items : check_items){
Log.v(LOG_TAG,items+"new");
}
return check_items;
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String[] result) {
if (result != null) {
for (String resultItems : result){
Log.v(LOG_TAG,resultItems+"GoodLuck");
final String PICTURE_BASE_URL = "http://image.tmdb.org/t/p/w185/";
final String PICTURE_URL_END = resultItems;
Uri builtUri = Uri.parse(PICTURE_BASE_URL).buildUpon()
.appendPath(PICTURE_URL_END.replace("/",""))
.build();
Log.v(LOG_TAG,resultItems+"GoodLuck"+builtUri.toString());
imageAdapter.setmresultItems(builtUri.toString());
}
}
}
}
}
class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mresultItems;
public ImageAdapter(Context c,ArrayList<String> resultItems){
mContext = c;
mresultItems = resultItems;
}
public int getCount(){
// return mThumbsIds.length;
return 0;
}
public Object getItem(int position){
return null;
}
public long getItemId(int position){
return 0;
}
public void setmresultItems(String resultItems){
mresultItems.add(resultItems);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null){
// if it is not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
}else{
imageView = (ImageView) convertView;
}
//imageView.setImageResource(mThumbsIds[position]);
for(String picResultUrl : mresultItems){
Picasso.with(mContext).load(picResultUrl).into(imageView);
}
return imageView;
}
//references to our Images
//private Integer[] mThumbsIds ;
}**
You shouldn't have a for loop in the getView() method, it already gets called for each item in the list.
Be sure to return the correct count:
public int getCount(){
return mresultItems.size();
}
Then, use just the current position of the data source in getView():
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
}else{
imageView = (ImageView) convertView;
}
//modified:
String picUrl = mresultItems.get(position);
Picasso.with(mContext).load(picUrl).into(imageView);
}
return imageView;
}
Also, call notifyDataSetChanged() on the adapter in onPostExecute() after the data source of the adapter has been updated:
#Override
protected void onPostExecute(String[] result) {
if (result != null) {
for (String resultItems : result){
Log.v(LOG_TAG,resultItems+"GoodLuck");
final String PICTURE_BASE_URL = "http://image.tmdb.org/t/p/w185/";
final String PICTURE_URL_END = resultItems;
Uri builtUri = Uri.parse(PICTURE_BASE_URL).buildUpon()
.appendPath(PICTURE_URL_END.replace("/",""))
.build();
Log.v(LOG_TAG,resultItems+"GoodLuck"+builtUri.toString());
imageAdapter.setmresultItems(builtUri.toString());
}
//Add this here:
imageAdapter.notifyDataSetChanged();
}
}
I found that i was not returning view in onCreate method which was the root cause of blank screen.
I want to show downloaded images in a ListView. Images are downloaded with a function DownloadImage and are bitmaps.
How do I show this in a ListView?
Images stored in folder htdocs in xampp and name images with book_id in a table book are equal(book_id=100 and name image=100.png).I want each book to have its own image.
I can show in the ListView book_name and book_price. The problem is with the image book.
class:
package bookstore.category;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.squareup.picasso.Picasso;
import android.R.drawable;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import bookstore.pack.JSONParser;
import bookstore.pack.R;
import android.app.Activity;
import android.app.ProgressDialog;
public class Computer extends Activity {
//Bitmap bm = null;
// progress dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> computerBookList;
private static String url_books = "http://10.0.2.2/project/computer.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_BOOK = "book";
private static final String TAG_BOOK_NAME = "book_name";
private static final String TAG_BOOK_PRICE = "book_price";
private static final String TAG_BOOK_ID = "book_id";
private static final String TAG_MESSAGE = "massage";
// category JSONArray
JSONArray book = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
Typeface font1 = Typeface.createFromAsset(getAssets(),
"font/bnazanin.TTF");
// Hashmap for ListView
computerBookList = new ArrayList<HashMap<String, String>>();
new LoadBook().execute();
}
class LoadBook extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Computer.this);
pDialog.setMessage("Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<Book> books=new ArrayList<Book>();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_books, "GET", params);
// Check your log cat for JSON reponse
Log.d("book:", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// books found
book = json.getJSONArray(TAG_BOOK);
for (int i = 0; i < book.length(); i++) {
JSONObject c = book.getJSONObject(i);
// Storing each json item in variable
String book_name = c.getString(TAG_BOOK_NAME);
String book_price = c.getString(TAG_BOOK_PRICE);
String book_id = c.getString(TAG_BOOK_ID);
DownloadImage("10.0.2.2/project/images
/"+book_id+".png");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_BOOK_NAME, book_name);
map.put(TAG_BOOK_PRICE, book_price);
// map.put(TAG_AUTHOR_NAME, author_name);
// adding HashList to ArrayList
computerBookList.add(map);
}
return json.getString(TAG_MESSAGE);
} else {
System.out.println("no book found");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
ListView view1 = (ListView) findViewById(R.id.list_view);
public void run() {
ImageView iv = (ImageView) findViewById(R.id.list_image);
ListAdapter adapter = new SimpleAdapter(Computer.this,
computerBookList, R.layout.search_item,
new String[] { TAG_BOOK_NAME, TAG_BOOK_PRICE },
new int[] { R.id.book_name, R.id.book_price });
view1.setAdapter(adapter);
view1.setAdapter(adapter);
}
});
}
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
}
For experience better modularity, first of all define a Book class with its properties and methods, For example here is an option:
public class Book {
String mName;
long mPrice;
Bitmap mPhoto;
}
Then collect all of your book information into a list of Book(i.e List<Book>).
Now it's time to declare a custom adapter for the ListView. For example you can try:
public class CustomAdapter extends ArrayAdapter<Book> {
public CustomAdapter(Context context, List<Book> books) {
super(context, 0, books);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null) {
// inflate row layout and assign to 'row'
}
final thisBook = getItem(position);
final ImageView photo = row.findViewById(R.id.photo);
photo.setImageBitmap(thisBook.mPhoto);
return row;
}
}
Use any library for this.
I recommend to use picasso library
just download the latest jar file from here
and use this code in your getView() method of your adapter
Picasso.with(context)
.load(url)
.placeholder(R.drawable.img_loading)
.error(R.drawable.img_error)
.into(imageView);
First store the bitmaps (from AsyncTask) to an Arraylist or any data structure. Then you have make a Custom adapter extending the "BaseAdapter" class and set it on your Listview so that you can show those bitmaps in your list.
Implementing custom adapters can be found here
I 'm having a problem with my gallery, it's to slow and it lags. I 'm loading images from server with Image Adapter:
package com.example.ocenitaksi;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.apache.http.client.utils.URLEncodedUtils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
public ImageAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
// ubacivanje slika za string
String mobile = mobileValues[position];
if (mobile.equals(MainActivity.imena[position])) {
//imageView.setImageURI("http://24.media.tumblr.com/avatar_a400b4dbb80e_64.png");
Bitmap bitmap=null;;
mobile = mobile.replace(" ", "_");
bitmap = DownloadImage("http://android.yunews.net/images/"+mobile+".png");
imageView.setImageBitmap(bitmap);
}
}
else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return mobileValues.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
}
and this is part of my main activity.
private class LongOperation extends AsyncTask<String, Void, String>
{
protected void onPreExecute()
{
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Molimo sacekajte...");
progressDialog.setMessage("Ucitavam taksi sluzbe...");
progressDialog.setCancelable(true);
progressDialog.show();
}
protected String doInBackground(String... params)
{
niz = new JSONFunkcije().ListaSluzbi();
imena= new String[niz.size()];
Iterator<String> it= niz.iterator();
for (int i=0;i<niz.size();i++)
imena[i]=it.next();
slicice = new ImageAdapter(MainActivity.this, imena);
return null;
}
protected void onPostExecute(String result)
{
progressDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
//Button dugme1 = (Button) findViewById(R.id.dugme1);
///params.height = "50dpi";
//dugme1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100));
Gallery gridView = (Gallery) findViewById(R.id.gallery1);
gridView.setAdapter(slicice);
//kraj ucitvanja
//gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), TaksiDetalji.class);
// passing array index
i.putExtra("id", imena[position]);
startActivity(i);
}
});
}
});
System.gc();
}
}
I 'm calling LongOperation from OnCrate:
mytask = new LongOperation();
mytask.execute();
Is something wrong with ImageAdapter? Too many calls, downloads? I tried to cache images but I failed.
From your code, I would say there are many reasons and because all these below reasons its running slow:
You haven't followed ViewHolder pattern. Read documentation also for Making ListView Scrolling Smooth
You haven't followed standard way of loading images (i.e. Cached images in local, load from local if its already cached)
Better you try any library available (Universal Image loader or any) on web to load images in your ListView.