网络图片查看器

    xiaoxiao2021-12-14  17

    一、运行效果图

    二、核心代码

    package com.example.imagebrowser import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private EditText editText; private ImageView imageView; private static final int SHOW_IMAGE = 1; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case SHOW_IMAGE: Bitmap bitmap = (Bitmap) msg.obj; imageView.setImageBitmap(bitmap); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText); imageView = (ImageView) findViewById(R.id.imageView); } public void showImage(View view) { final String path = editText.getText().toString(); if (TextUtils.isEmpty(path)) { Toast.makeText(this, "图片不能为空", Toast.LENGTH_LONG).show(); } else { new Thread() { public void run() { try { URL url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(5000); int responsecode = httpURLConnection.getResponseCode(); if (responsecode == 200) { InputStream is = httpURLConnection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); Message msg = new Message(); msg.what = SHOW_IMAGE; msg.obj = bitmap; handler.sendMessage(msg); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }; }.start(); } } } <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" /> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:text="http://pica.nipic.com/2007-11-09/2007119121849495_2.jpg" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:onClick="showImage" android:text="浏览" /> </LinearLayout> 三、运行中遇到的问题

    刚开始运行程序,闪退。代码有问题。后来修改了一下程序,运行成功。

    转载请注明原文地址: https://ju.6miu.com/read-972117.html

    最新回复(0)