Reputation: 29
My LoadImageTask class:
private class LoadImageTask extends AsyncTask<Integer,Void,String>
{
String string;
WeakReference<ImageView> imageViewWeakReference;
public LoadImageTask(ImageView imageView) {
imageViewWeakReference = new WeakReference<ImageView>(imageView);
}
@Override
protected String doInBackground(Integer... params) {
//get image default
mService.getExhibitImageById(params[0], true).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
try{
string = response.body();
Log.d("AnswersPresenter", "Image loaded!!!!");
}
catch (Exception e)
{
e.printStackTrace();
}
} else {
int statusCode = response.code();
Toast.makeText(mContext, "Error" + statusCode + response.message(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
showErrorMessage();
Log.d("AnswersPresenter", "error loading image!!!");
}
});
return string;
}
@Override
protected void onPostExecute(String string) {
super.onPostExecute(string);
if (string !=null && imageViewWeakReference !=null)
{
ImageView imv =imageViewWeakReference.get();
if (imv!=null)
{
Bitmap bitmap = Util.StringToBitMap(string);
imv.setImageBitmap(bitmap);
}
}
}
}
I use retrofit inside method doInBackground()
to get a string encoded of image by id from api: http://demo.museum.vebrary.vn/api/Exhibit/GetImage?id=3
With id is got in
@Override
public void onBindViewHolder(Adapter.ViewHolder holder, int position) {
ExhibitMainScreenModel item = ExhibitList.get(position);
TextView tvName = holder.tvName;
tvName.setText(item.getEXHIBITNAME());
TextView tvDesc = holder.tvDescription;
tvDesc.setText(item.getDESCRIPTION());
ImageView imv = holder.imvExhibit;
/*//get ID and load image by id
id = item.getEXHID();
loadImage(id,holder);*/
id = item.getEXHID();
LoadImageTask task = new LoadImageTask(holder.imvExhibit);
task.execute(id);
}
doInBackground return a string encode of image and in void onPostExecute(String string)
I use
public static Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length,options);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
change it to bitmap to display imageview.
But doInBackGround return string = null;
While I debug, string in
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
try{
string = response.body();
Log.d("AnswersPresenter", "Image loaded!!!!");
}
catch (Exception e)
{
e.printStackTrace();
}
} else {
int statusCode = response.code();
Toast.makeText(mContext, "Error" + statusCode + response.message(), Toast.LENGTH_SHORT).show();
}
string = "/9j/4AAQSkZJRgABAgEASABIAAD/4RG1RXhpZgA..."
, this is a encoded string. i don't know why string in return was null?
Can you tell me the right way?
thank you very much!
Upvotes: 1
Views: 482
Reputation: 1
Method enqueue(Callback callback) asynchronously send the request and notify callback of its response or if an error occurred talking to the server, creating the request, or processing the response.
In your case "return string" get called before new Callback() class
Upvotes: 0