Android Network Picture View Display implementation method

Source: Internet
Author: User

As follows:

There are three controls in the interface, one edittext, one button, and one ImageView

1, the following is the specific layout file

<EditText  android:id="@+id/picturepagh"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/hello_world" /><Button  android:id="@+id/btn"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="查看" /><ImageButton  android:id="@+id/imageView"  android:layout_width="fill_parent"  android:layout_height="200px" />2, in mainactivity the picture diagram code writingpublic class MainActivity extends Activity {private Button btn;private EditText path;private ImageView imgview;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn = (Button) findViewById(R.id.btn);path = (EditText) findViewById(R.id.picturepagh);imgview = (ImageView) findViewById(R.id.imageView);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Log.i("CLICK", ((Button) v).getText().toString());new Thread(runa).start();}});}public void setView() {String picturepath = path.getText().toString();byte[] data = null;try {data = ImageService.getImage(picturepath);Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// BitmapFactory:图片工厂!Looper.prepare();// 必须调用此方法,要不然会报错Message msg = new Message();msg.what = 0;msg.obj = bitmap;handler.sendMessage(msg);} catch (Exception e) {Toast.makeText(getApplicationContext(), "获取图片错误", 1).show();}}private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (msg.what == 0) {updateImageView((Bitmap) msg.obj);}}};private Runnable runa = new Runnable() {@Overridepublic void run() {setView();}};private void updateImageView(Bitmap bm) {imgview.setImageBitmap(bm);}}3, add a ImageService image service class, which contains a method to obtain network data;public class ImageService {// 获取网络图片的数据public static byte[] getImage(String picturepath) throws Exception {URL url = new URL(picturepath);HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 基于http协议的连接对象conn.setConnectTimeout(10);// 10秒;conn.setRequestMethod("GET");// 大写if (conn.getResponseCode() == 200) {InputStream ins = conn.getInputStream();return StreamTool.read(ins);}return null;}}4. Add a stream processing work class Streamtoolpublic class StreamTool {public static byte[] read(InputStream ins) throws Exception {ByteArrayOutputStream outstream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int length = 0;while ((length = ins.read(buffer)) > -1) {outstream.write(buffer, 0, length);}outstream.close();return outstream.toByteArray();}}where this procedure is not appropriate, it needs to be modified.

Android Network Picture View Display implementation method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.