1

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

问题是那部分...

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

3 回答 3

2

您在 doInBackground 中创建了一个您从不使用的位图。而是返回位图并在 onPostExecute 中使用它。

于 2012-10-26T11:26:12.420 回答
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;
    }
}
于 2012-10-26T12:37:52.517 回答
0

嘿,我在从 url 显示图像时遇到了同样的问题,这个链接非常有用。你也可以参考stackoverflow讨论

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

干杯!

于 2014-07-10T09:31:08.780 回答