使用setImageBitmap从图像Web使用AsyncTask ImageView

时间:2012-10-26 11:19:11

标签: android

我在互联网上显示此图片时遇到问题。 我不知道如何使它工作。 我是android的新手。

问题在于......

imView = (ImageView) findViewById(R.id.imageView1); 
imView.setImageBitmap(bm); //error

谢谢。

我的代码

public class CarregaImagem extends AsyncTask<String, Void, String>{
    String imageUrl = "http://www.cuboweb.com.br/android/images/logoconsulfarma.png";
    private ProgressDialog progress;
    private Activity activity;
    Bitmap bmImg;

    public CarregaImagem(Activity activity){
        this.activity = activity;
    }

    protected void onPreExecute() {
        progress = new ProgressDialog(activity);
        progress.setTitle("Aguarde...");
        progress.setMessage("Carregando..."); 
        progress.show();    
    }

    protected String doInBackground(String... params) { 
        // TODO Auto-generated method stub
        try { 
            URL aURL = new URL(imageUrl);
            final URLConnection conn = aURL.openConnection(); 
            conn.connect();
            final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
            final Bitmap bm = BitmapFactory.decodeStream(bis);
            BitmapFactory.decodeStream(new URL(imageUrl).openConnection().getInputStream()); 
            bis.close();
        } catch (IOException e) { 
            imageUrl = "";
        } catch(Exception f){
            imageUrl = "";
        }
        return imageUrl;        
    }

    protected void onPostExecute(String imageUrl) {

        if(!imageUrl.equals("")){
            imView = (ImageView) findViewById(R.id.imageView1); 
            imView.setImageBitmap(bm); //error 
        } else{
            Toast.makeText(activity, "Não foi possível obter resultados", Toast.LENGTH_LONG).show();
        }           
        progress.dismiss();         
    }   
}

3 个答案:

答案 0 :(得分:2)

您在doInBackground中创建一个您从未使用过的位图。 返回位图并在onPostExecute中使用它。

答案 1 :(得分:1)

尝试以下代码从网上下载图片并在imageview中显示。

public class MainActivity extends Activity {

    ImageView mImgView1;
    static Bitmap bm;
    ProgressDialog pd;
    String imageUrl = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg";
    BitmapFactory.Options bmOptions;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImgView1 = (ImageView) findViewById(R.id.mImgView1);
        pd = ProgressDialog.show(MainActivity.this, "Aguarde...",
                "Carregando...");
        new ImageDownload().execute("");
    }

    public class ImageDownload extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;
            loadBitmap(imageUrl, bmOptions);
            return imageUrl;
        }

        protected void onPostExecute(String imageUrl) {
            pd.dismiss();
            if (!imageUrl.equals("")) {
                mImgView1.setImageBitmap(bm);
            } else {
                Toast.makeText(MainActivity.this,
                        "Não foi possível obter resultados", Toast.LENGTH_LONG)
                        .show();
            }
        }

    }

    public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bm = BitmapFactory.decodeStream(in, null, options);
            in.close();
        } catch (IOException e1) {
        }
        return bm;
    }

    private static InputStream OpenHttpConnection(String strURL)
            throws IOException {
        InputStream inputStream = null;
        URL url = new URL(strURL);
        URLConnection conn = url.openConnection();

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = httpConn.getInputStream();
            }
        } catch (Exception ex) {
        }
        return inputStream;
    }
}

答案 2 :(得分:0)

嘿,我遇到了从网址this link was very useful显示图片的同样问题。你也可以参考 stackoverflow discussion

显然,android 3以后网络连接严格,因此网络连接失败

干杯!