package xiaosi.https; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class HttpsActivity extends Activity { private Button button = null; private ImageView image = null; private ProgressDialog dialog = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ button = (Button)findViewById(R.id.button); button.setOnClickListener(new buttonListener()); image = (ImageView)findViewById(R.id.image); dialog = new ProgressDialog(HttpsActivity.this); dialog.setMessage("正在加载中..."); } private class buttonListener implements OnClickListener{ @Override public void onClick(View v) { try { if (!dialog.isShowing()) { dialog.show(); } image.setImageBitmap(httpsGetBitmap()); if (dialog.isShowing()) { dialog.dismiss(); } } catch (Exception e) { e.printStackTrace(); } } } private Bitmap httpsGetBitmap() throws Exception{ URL url = new URL("http://srimg1.meimei22.com/pic/suren/2012-3-26/1/1.jpg"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置是否从httpUrlConnection读入,默认情况下是true; connection.setDoInput(true); InputStream inputSream = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputSream); inputSream.close(); return bitmap; } }
Android进阶2之Https访问网络资源(获取网络图片)
最新推荐文章于 2024-04-21 23:54:17 发布