asynctask with callback - cant display images

时间:2015-03-03 16:49:44

标签: java android android-asynctask callback

无法显示图像..除非在回调函数中添加添加代码。但是因为我要循环绘制操作,我只是使用了赋值 imageHandler = image;它似乎不起作用

活动类:

public class Home extends ActionBarActivity implements OnTaskComplete{


public Bitmap imageHandler;

@Override
public void callBackFunction(Bitmap image) {

     imageHandler = image;

}

public class Post{

    String id;
    String title;
    String description;
    String release;

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public String getRelease() {
        return release;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setRelease(String release) {
        this.release = release;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {

        return id;
    }

}

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    Context context = this;


    String result = null;
    ArrayList<Post> focusOn = new ArrayList<Post>();


    try {
    URL address = new URL("http://www.youth-stories.com/api/all.php");
    URLDataReader reader = new URLDataReader(context);
    result = reader.execute(address).get();

}catch (IOException e){
    e.printStackTrace();
} catch(InterruptedException e){
    e.printStackTrace();
} catch (ExecutionException e){
    e.printStackTrace();
}

    try {
        JSONObject obj = new JSONObject(result);
        String success = (String) obj.getString("success");
        JSONArray records = obj.getJSONArray("records");

        for(int i = 0; i < records.length(); i++) {
            Post tmp = new Post();
            tmp.setId(records.getJSONObject(i).getString("id"));
            tmp.setTitle(records.getJSONObject(i).getString("title"));
            tmp.setDescription(records.getJSONObject(i).getString("contents"));
            tmp.setRelease(records.getJSONObject(i).getString("data_post"));
            focusOn.add(tmp);
        }

    }catch (JSONException e){
        e.printStackTrace();
    }


    //wrapper
    LinearLayout container = (LinearLayout)findViewById(R.id.wrapper);

    for(int i = 0; i < focusOn.size(); i++){
        //item
        LinearLayout item = new LinearLayout(getApplicationContext());
        container.addView(item);
        item.setOrientation(LinearLayout.HORIZONTAL);

        //image
        //Bitmap imageHandler = null;
        URL address = null;

       try {

           address = new URL("http://www.youth-stories.com/public/admin/CH_FocusOn/images/"+focusOn.get(i).getId()+"_thumb2.jpg");
           URLImageReader reader = new URLImageReader(context,this);
           reader.execute(address);


       }catch(MalformedURLException e){
           e.printStackTrace();
       }


        ImageView asset = new ImageView(getApplicationContext());
        asset.setImageBitmap(imageHandler);
        //Toast.makeText(getApplicationContext(),asset.toString(),Toast.LENGTH_LONG).show();
        item.addView(asset);

        LinearLayout.LayoutParams imgSettings = new LinearLayout.LayoutParams(300,300);
        asset.setLayoutParams(imgSettings);
        //inside
        LinearLayout contents = new LinearLayout(getApplicationContext());
        contents.setOrientation(LinearLayout.VERTICAL);
        item.addView(contents);
        //title
        TextView title = new TextView(getApplicationContext());
        title.setText(focusOn.get(i).getTitle());
        title.setTextAppearance(this, R.style.title);
        contents.addView(title);
        //description
        TextView description = new TextView(getApplicationContext());
        description.setText(focusOn.get(i).getDescription());
        description.setTextAppearance(this, R.style.description);
        contents.addView(description);
        //date
        TextView date = new TextView(getApplicationContext());
        date.setText(focusOn.get(i).getRelease());
        date.setTextAppearance(this, R.style.description);
        contents.addView(date);

    }

}

}

URLImageReader Asynctask

public class URLImageReader extends AsyncTask<URL, Void, Bitmap> {

Context context = null;

private OnTaskComplete mlistener;

public URLImageReader(Context context, OnTaskComplete mlistener){
    this.context = context;
    this.mlistener = mlistener;
}


@Override
protected Bitmap doInBackground(URL... params) {

    Bitmap image = null;

    try {
        URL url= params[0];
        image = BitmapFactory.decodeStream(url.openConnection().getInputStream());


    } catch (IOException e){
        e.printStackTrace();
    }

    return image;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
    super.onPostExecute(bitmap);
    mlistener.callBackFunction(bitmap);
}

接口:

public interface OnTaskComplete{
    void callBackFunction(Bitmap image);
}

1 个答案:

答案 0 :(得分:0)

你在任务完成界面上似乎无关紧要,只需将imageview传递给它:

public class URLImageReader extends AsyncTask<URL, Void, Bitmap> {

    private final Context context;        
    private final ImageView mImageView;

    public URLImageReader(ImageView imageView){
        this.context = imageView.getContext();
        this.mImageView = imageView;
    }

    @Override
    protected Bitmap doInBackground(URL... params) {
        Bitmap image = null;
        try {
            URL url= params[0];
            image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (IOException e){
            e.printStackTrace();
        }

        return image;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        if (mImageView != null){
            mImageView.setImageBitmap(bitmap);
        }
    }
}

然后调用它,

    // see final notes below about using this (the activity),  lets create the imageview before we call URLImageReader constructor 
    ImageView asset = new ImageView(this);
   try {

       address = new URL("http://www.youth-stories.com/public/admin/CH_FocusOn/images/"+focusOn.get(i).getId()+"_thumb2.jpg");
       URLImageReader reader = new URLImageReader(asset);
       reader.execute(address);


   }catch(MalformedURLException e){
       e.printStackTrace();
   }

另外,不要使用应用程序上下文来创建视图,而是使用Activity上下文。