Hi I developing Android java app I am just learning Android Java. The image in the string data that I get from the API is not loaded. I am using the Picasso class to load the image. While the image can be loaded in Imageview, the image tag defined in the String is not pulled I tried using Glide and Picasso class as a solution but I couldn't find any more solutions because I got an error. ContentDetail.java
public class ContentDetail extends AppCompatActivity {
ContentDetailBinding makDet;
MakalelerAdapter contentsAdp;
CompositeDisposable compositeDisposable;
Retrofit retrofit;
ArrayList<ContentsApi> content;
private String BASE_URL = "https://aa.com.tr/";
@Override
protected void onCreate(Bundle savedInstanceState) {
makDet=ContentDetailBinding .inflate(getLayoutInflater());
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
Gson gson = new GsonBuilder().setLenient().create();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
loadData();
setContentView(makDet.getRoot());
}
private void loadData(){
final Contents contentApi = retrofit.create(Contents.class);
compositeDisposable = new CompositeDisposable();
int getId=getIntent().getIntExtra("content_id",0);
Log.i("id",String.valueOf(getId));
compositeDisposable.add(contentApi.getContent(getId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleResponse, Throwable::printStackTrace)
);
}
private void handleResponse(List<ContentsApi> contentData) {
Log.i("dxx",contentData.get(0).content_image);
if(contentData.isEmpty()){
Snackbar.make(makDet.getRoot(),"İçerik Bulunamadı",Snackbar.LENGTH_LONG).show();
}else{
Log.i("dxx",contentData.get(0).content_image);
ContentsApi content = new ArrayList<>(contentData).get(0);
TextView info = makDet.detailInfo;
// info.setText(Html.fromHtml(content.content_text,Html.FROM_HTML_MODE_COMPACT));
Spannable html;
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.i("Hata bimp","hata2");
html = (Spannable) Html.fromHtml(content.content_text, Html.FROM_HTML_MODE_LEGACY, new PicassoImageGetter(info), null);
} else {
Log.i("hata bimp","hata");
html = (Spannable) Html.fromHtml(content.content_text, new PicassoImageGetter(info), null);
}
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
html = (Spannable) Html.fromHtml(content.content_text, Html.FROM_HTML_MODE_LEGACY, new PicassoImageGetter(info), null);
} else {
html = (Spannable) Html.fromHtml(content.content_text, new PicassoImageGetter(info), null);
}
info.setText(html);
Picasso.get().load(Uri.parse(content.content_image)).fit().into(imageView);
}
}
}
PicassoImageGetter.java class
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;
public class PicassoImageGetter implements Html.ImageGetter {
private TextView textView;
public PicassoImageGetter(TextView textView) {
this.textView = textView;
}
public Drawable getDrawable(String source) {
// Use Glide or Picasso to load the WebP image
Glide.with(textView.getContext())
.load(source)
.into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
resource.setBounds(0, 0, resource.getIntrinsicWidth(), resource.getIntrinsicHeight());
textView.invalidate();
textView.setText(textView.getText()); // Refresh the TextView
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
// Handle placeholder if needed
}
});
return null; // Return null to indicate that the drawable is not ready yet
}
//@Override
public Drawable getDrawable2(String source) {
// Use Picasso or any other image loading library to load the image
// Example with Picasso:
Picasso.get().load(source).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable drawable = new BitmapDrawable(textView.getResources(), bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
textView.invalidate();
textView.setText(textView.getText()); // Refresh the TextView
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Log.i("Hata bimp",e.getMessage());
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
// Optional: Set a placeholder
}
});
return null; // Return null to indicate that the drawable is not ready yet
}
}
Hi I developing Android java app The image in the string data that I get from the API is not loaded. I am using the Picasso class to load the image. While the image can be loaded in Imageview, the image tag defined in the String is not pulled
Source: View source