【问题标题】:How to set bitmap on ImageView java android如何在ImageView java android上设置位图
【发布时间】:2014-12-06 17:06:53
【问题描述】:

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

这得到一个位图。

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";
    }
}

这个设置图像视图

new picture_get().execute();

runOnUiThread(new Runnable() {

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

});

所有代码都可以工作,但图像仍然相同。

【问题讨论】:

  • 您能具体说明您要做什么吗?
  • 很难说,但在我看来,从您发布的代码来看,您是在加载图像之前设置图像。你不应该在AsyncTask 中覆盖onPostExecute,然后从那里调用runOnUIThread(...) 吗?
  • 我试图从 url 获取图片并将其设置在 imageview 上 :)

标签: java android bitmap imageview android-drawable


【解决方案1】:

我建议在您控制 ImageView 的类中实现一个接口。 而不是从你的 onPostExecute(Bitmap 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;
    }
}

}

【讨论】:

    【解决方案2】:

    这应该可以工作

    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);       
        }
    }
    

    【讨论】:

      【解决方案3】:
      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);
      }
      

      }

      【讨论】:

        猜你喜欢
        • 2013-02-25
        • 2015-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-10
        • 1970-01-01
        相关资源
        最近更新 更多