如何在ImageView java android上设置位图

时间:2014-12-06 17:06:53

标签: java android bitmap imageview android-drawable

我有一个代码从url获取位图然后更改imageview但我不知道为什么这不起作用..我尝试了一些类似的答案,但图像仍然没有设置。

这是一个位图。

class picture_get extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        URL img_value = null;
        try {
            img_value = new URL("https://graph.facebook.com/"+id+"picture?type=large");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            bitmap_pic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
            Log.i("bitmap_pic_get", "ok");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //bitmap_pic = Bitmap.createScaledBitmap(bitmap_pic, 50, 50, false);
        return "Executed";
    }
}

这设置了imageview

new picture_get().execute();

runOnUiThread(new Runnable(){

@Override
public void run() {
    textView1.setText("Id: "+ id);
    image1.setImageBitmap(bitmap_pic);
}

});

所有代码都在工作但图像仍然相同。

3 个答案:

答案 0 :(得分:2)

我建议在控制ImageView的类中实现一个接口。 而不是从你的onPostExecute(位图bmp)调用它 :

public class ImageDownloader
{
public interface ImageDownload
{
    void getImage(Bitmap bmp);
}

Connection conn;
ImageDownload callBack;

public ImageDownloader(ImageDownload cb)
{
    conn = new Connection();
    this.callBack = cb;
}

public AsyncTask<String, Void, Bitmap> downloadFile(ObjectType objectType, int fileID, int width,
                                                    int height, int fitMode)
{
    AsyncTask<String, Void, Bitmap> task = null;
    String url = getURL(objectType,fileID,width,height,fitMode);
    try
    {
            task = new Downloader().executeOnExecutor(
                    AsyncTask.THREAD_POOL_EXECUTOR, url);
    }
    catch (Exception e)
    {
        callBack.getImage(null);
    }

    return task;
}

String getURL(ObjectType objectType, int fileID, int width,
              int height, int fitMode)
{
    String url = conn.URL + FUNCTION
            + "?" + PARAM_OBJECT_TYPE + "=" + objectType.ObjectType()
            + "&" + PARAM_FILE_ID + "=" + fileID
            + "&" + PARAM_WIDTH + "=" + width
            + "&" + PARAM_HEIGHT + "=" + height
            + "&" + PARAM_FIT_MODE + "=" + fitMode;

    return url;
}

private class Downloader extends AsyncTask<String, Void, Bitmap>
{
    @Override
    protected void onPostExecute(Bitmap b)
    {
        super.onPostExecute(b);
        callBack.getImage(b);
    }

    @Override
    protected Bitmap doInBackground(String... params)
    {
        //android.os.Debug.waitForDebugger();
        Bitmap bmp = null;
        try
        {
            URL url = new URL(params[0]);

            URLConnection conn = url.openConnection();

            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setDoInput(true);


            InputStream input = conn.getInputStream();
            bmp = BitmapFactory.decodeStream(input);
        } catch (MalformedURLException e) {

        } catch (IOException e) {

        }

        return bmp;
    }
}

}

答案 1 :(得分:1)

这应该有效

class picture_get  extends AsyncTask<String, Bitmap, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... params) {
        URL img_value = null;
        try {
            img_value = new URL("https://graph.facebook.com/"+id+"picture?type=large");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            bitmap_pic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
            Log.i("bitmap_pic_get", "ok");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //bitmap_pic = Bitmap.createScaledBitmap(bitmap_pic, 50, 50, false);
        return bitmap_pic ;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap_pic) {
    image1.setImageBitmap(bitmap_pic);       
    }
}

答案 2 :(得分:0)

class picture_get extends AsyncTask<String, Void, Bitmap> {

@Override
protected String doInBackground(String... params) {
    URL img_value = null;
    try {
        img_value = new URL("https://graph.facebook.com/"+id+"picture?type=large");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        bitmap_pic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
        Log.i("bitmap_pic_get", "ok");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //bitmap_pic = Bitmap.createScaledBitmap(bitmap_pic, 50, 50, false);
    return bitmap_pic
}
@Override
void onPostExecute(Bitmap result){

   image1.setImageBitmap(bitmap_pic);
}

}