**Hello, I am using Picasso
to load images that are obtained from a HTML
with the following url as an example: Link . When extracting the url of the images, I get them perfectly:
To verify that the urls worked correctly, I entered them in the browser and they load correctly: Image link
But, when I want to show them in my application, absolutely nothing happens, the screen goes blank:
This is the activity code:
private RecyclerView recyclerView;
private TMOnlineLectorAdaptador adapter;
private ArrayList<TMOLectorClase> tmoLectorClases = new ArrayList<>();
private String url = "";
private TimerTask _timerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tmonline_lector);
url = getIntent().getStringExtra("url");
recyclerView = findViewById(R.id.rvCapitulosSeleccion);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new TMOnlineLectorAdaptador(tmoLectorClases, TMOnlineLector.this);
recyclerView.setAdapter(adapter);
Content content = new Content();
content.execute();
}
private class Content extends AsyncTask<Void,Void, ArrayList<TMOLectorClase>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(ArrayList<TMOLectorClase> items) {
super.onPostExecute(items);
//Actualizar información
adapter.updateData(items);
adapter.notifyDataSetChanged();
}
@Override
protected ArrayList<TMOLectorClase> doInBackground(Void... voids) {
tmoLectorClases.clear();
try {
String nuevaUrl = Jsoup.connect(url).followRedirects(true).execute().url().toExternalForm();
Log.d("Items", "Url: " + nuevaUrl);
if(nuevaUrl.contains("/paginated")){
nuevaUrl = nuevaUrl.replaceAll("/paginated", "/cascade");
Log.d("URLS", "doInBackground: " + nuevaUrl);
Document doc = Jsoup.connect(nuevaUrl).get();
Log.d("Items", "Url: " + doc);
Elements data = doc.select("div.img-container.text-center");
for (Element e : data){
String imgUrl = "";
if(e.select("div.img-container.text-center").size() > 0)
imgUrl = e.select("img").get(0).attr("data-src");
Log.d("finalurl", "url nueva:" + nuevaUrl + "\nimagenes: " + imgUrl);
//String imgUrl = e.select("img").attr("data-src");
tmoLectorClases.add(new TMOLectorClase(imgUrl));
}
}else{
Document doc = Jsoup.connect(nuevaUrl).get();
Elements data = doc.select("div.img-container.text-center");
for (Element e : data){
String imgUrl = e.select("img").attr("data-src");
Log.d("finalurl", "url nueva:" + nuevaUrl + "\nimagenes: " + imgUrl);
tmoLectorClases.add(new TMOLectorClase(imgUrl));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return tmoLectorClases;
}
}
This is the adapter code:
public class TMOnlineLectorAdaptador extends RecyclerView.Adapter<TMOnlineLectorAdaptador.ViewHolder>{
private ArrayList<TMOLectorClase> tmoLectorClases;
private Context context;
PhotoViewAttacher mAttacher;
public TMOnlineLectorAdaptador(ArrayList<TMOLectorClase> tmoItems, Context context) {
this.tmoLectorClases = tmoItems;
this.context = context;
}
@NonNull
@Override
public TMOnlineLectorAdaptador.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adaptador_lectortmo, parent, false);
return new TMOnlineLectorAdaptador.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final TMOnlineLectorAdaptador.ViewHolder holder, int position) {
TMOLectorClase tmoLectorClase = this.tmoLectorClases.get(position);
Picasso.get().load(tmoLectorClase.getImg()).into(holder.iv);
}
@Override
public int getItemCount() {
return tmoLectorClases.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView iv;
public ViewHolder(@NonNull View view) {
super(view);
iv = view.findViewById(R.id.ivPaginas);
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
}
}
public void setFilter(ArrayList<TMOLectorClase> newList) {
tmoLectorClases = new ArrayList<>();
tmoLectorClases.addAll(newList);
notifyDataSetChanged();
}
public void updateData(ArrayList<TMOLectorClase> items) {
this.tmoLectorClases = items;
}
}
Can anyone see the mistake I'm making? I don't know where to look for it
Update:
Debugeando
each line of my code, I thought that the problem was in my application when passing the link
by the array
implemented in the line of Picasso
. But, no, in fact even there they arrive well:
So I began to read all over the forum in both English and Spanish SO but I have not found any solution since there are times when the images load well and others when they do not. Example: today at 1:00 p.m. the images loaded fine, at 2:00 p.m. they were not displayed. Entering link
each image at 2:00 p.m. through the browser , they worked links
and loaded the images, but not in the application. So, I added this line to the Manifest
:
android:usesCleartextTraffic="true"
But, the problem persists. I tried using the library Glide
but that doesn't even load other images via URL
. Does anyone have or know of any other possible solution?
Update 2:
Adapter Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
style="@style/CardView.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:background="@color/blanco">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:background="@color/blanco">
<ImageView
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:id="@+id/ivPaginas"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
Activity Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.TMO.TMOnlineLector"
android:background="@color/blanco"
android:orientation="horizontal"
android:gravity="center_horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvLector"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
According to the image you show, the ArrayList
tmoLectorClaseArrayList
contains the images and you get the url according to the variableposition
which is correct:In the Adapter everything is correct, you create the Holder class where you get the ImageView reference:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { ImageView iv;
and in the method
onBindViewHolder
correctly loads the image inside theImageView
Which deduces that it can be two things:
First of all, it is important that you ensure that you have the Internet permission defined within the AndroidManifest.xml:
The problem is the layout, since the ImageView can have properties that cause it to not display correctly on the screen, for example if you use a ConstraintLatout.
Add your layout because this could be the problem that does not allow to display the images.
With respect to :
not necessary since image urls are
https://
.When reviewing the operation of your app in detail, the first thing that caught my attention is that it loaded a blank screen, go back and try again and I could see the images.
En realidad tu app no falla a menos que no tenga conexión a internet, pero da la impresión de que no va a cargar absolutamente nada, para evitar precisamente esta situación Google recomienda usar una barra de progreso (ProgressBar) de forma indeterminada en la interfaz de usuario para que el usuario sepa que esta esperando alguna respuesta.
Para esto te sugiero modificar la vista que cargaría el
RecyclerView
agregando unProgressBar
,tmonline_lector_adaptador.xml
:y dentro de tu Adapter
TMOnlineLectorAdaptador.java
modifica para agregar aPicasso
un Callback en el cual quitaría elProgressBar
cuando la Imagen se cargué, incluso puedes detectarobviamente agrega en tu clase
ViewHolder
la obtención de la referencia delProgresBar
:In this way you would obtain the following effect, an indicator would be displayed which indicates that the image is being loaded, in this way the user would not have the impression that nothing is being loaded.
Same for the Activity layout, add a ProgressBar:
and when the Asynctask responds you remove it: