Reputation: 1084
I want to display images from URL in a my application as a gallery. I have used this for reference.
http://www.androidhive.info/2016/04/android-glide-image-library-building-image-gallery-app/
But the images are not displayed.
This is my GalleryActivity
public class GalleryActivity extends AppCompatActivity {
String strServerResponse;
ProgressDialog nDialog;
private GalleryAdapter mAdapter;
private RecyclerView recyclerView;
private ArrayList<Image> images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
images = new ArrayList<>();
new NetCheck().execute();
}
private class NetCheck extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
nDialog = new ProgressDialog(GalleryActivity.this);
nDialog.setMessage("Loading..");
nDialog.setTitle("Please Wait");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.e("Post exec calleld", "dfds");
nDialog.dismiss();
mAdapter = new GalleryAdapter(getApplicationContext(), images);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(
uril);
httpRequest.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
httpRequest.setEntity(se);
HttpResponse httpRes = httpClient.execute(httpRequest);
java.io.InputStream inputStream = httpRes.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
strServerResponse = sb.toString();
Log.e("Server Response", "" + strServerResponse.toString());
if (strServerResponse != null) {
try {
JSONObject jsonObj = new JSONObject(strServerResponse);
String status = jsonObj.getString("status");
if (status.equals("true")) {
String result = jsonObj.getString("images");
Log.e("images", "" + eee);
JSONArray arr1 = new JSONArray(result );
JSONObject jsonObj1 = arr1.getJSONObject(0);
Image image = new Image();
for (int i = 0; i < arr1.length(); i++) {
JSONObject jobjj11 = arr1
.getJSONObject(i);
String name = jobjj11.optString("name");
Log.e("nurl",""+name);
image.setSmall(""+name);
images.add(image);
Log.e("images.tostring",""+images.toString());
}
}
else if (status.equals("false")){
Handler handler = new Handler(Looper.getMainLooper());
handler.post(
new Runnable() {
@Override
public void run() {
AlertDialog alertDialog = new AlertDialog.Builder(
GalleryActivity.this).create();
alertDialog.setMessage("No imagesFound");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
}
);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(
new Runnable() {
@Override
public void run() {
AlertDialog alertDialog = new AlertDialog.Builder(
GalleryActivity.this).create();
alertDialog.setMessage("Error Connecting to Server");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
//Log.e("ServiceHandler", "Couldn't get any data from the url");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
And this my GalleryAdapter
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.MyViewHolder> {
private List<Image> images;
private Context mContext;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbnail;
public MyViewHolder(View view) {
super(view);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
}
}
public GalleryAdapter(Context context, List<Image> images) {
Log.e("ingalleryadapter", "aaddd");
mContext = context;
this.images = images;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.gallery_thumbnail, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Image image = images.get(position);
Glide.with(mContext).load(image.getSmall())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.thumbnail);
}
@Override
public int getItemCount() {
return images.size();
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private GalleryAdapter.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final GalleryAdapter.ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
}
}
The images are not displayed. What is wrong here?
Upvotes: 2
Views: 839
Reputation: 1361
I had a similar issue with Glide. I had 2 approach which made the situation much more better.
1. Add holder.itemview.setTag("smthng unique")
because Glide uses tag to make relation.
2. Resize your images, if the images has huge pixel amount, it sometimes does not load so you need to downgrade pixels.
I hope this will work for you.
Upvotes: 0
Reputation: 1073
Check whether the images list has values. check by putting a log in getItemCount() method and check the size of the images list.
Upvotes: 0
Reputation: 5741
Try adding a call to override() and see if that fixes it. One common way this happens is if you're using views that don't have fixed sizes and/or are dependent on the size of their content. Glide tries to wait until views that don't have fixed sizes go through layout. In some cases the view ends up waiting on Glide and Glide ends up waiting on the view, resulting in the load silently never starting.
If override works, consider a fixed size in your view, or maybe paste your xml here so we can try to suggest other alternatives.
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
.into(imageViewResize);
For more details visit: Images fail to load in recyclerview #482
Upvotes: 4