今天要写一个网络图片查看器。
在网上随便照一张图片,复制它的地址后输入app中,在app中显示
界面如下:
布局十分简单
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_image"
android:layout_weight="500"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<EditText
android:text="http://c.hiphotos.baidu.com/image/pic/item/d788d43f8794a4c21f40b9c10af41bd5ac6e39fc.jpg"
android:id="@+id/et_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入图片路径"
/>
<Button
android:onClick="viewImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查看"
/>
</LinearLayout>
为了解决这个问题,需要添加layout_weight这个属性(不写出来则默认为1)
若将其值设置的越高,则其被渲染的优先级越低,即先将EditText和Button创建出来,最后剩余的空间就留给ImageView
这里为了方便,我提前将网络图片的路径设置好了
下面看一下MainActivity
package com.example.netimageviewer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
<span style="white-space:pre"> </span>private EditText et_path;
<span style="white-space:pre"> </span>private ImageView iv_image;
<span style="white-space:pre"> </span>private static String tag="MainActivity ";
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre"> </span>super.onCreate(savedInstanceState);
<span style="white-space:pre"> </span>setContentView(R.layout.activity_main);
<span style="white-space:pre"> </span>et_path = (EditText) findViewById(R.id.et_path);
<span style="white-space:pre"> </span>iv_image = (ImageView) findViewById(R.id.iv_image);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void viewImage(View view) {
<span style="white-space:pre"> </span>String path = et_path.getText().toString().trim();
<span style="white-space:pre"> </span>if (TextUtils.isEmpty(path)) {
<span style="white-space:pre"> </span>Toast.makeText(this, "图片路径不能为空", 1).show();
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>// 判断这个图片的路径是否已经缓存在sd卡上
<span style="white-space:pre"> </span>//lastIndexOf()方法可返回某个指定的字符串值在字符串中最后一次出现的位置。
<span style="white-space:pre"> </span>int start = path.lastIndexOf("/");
<span style="white-space:pre"> </span>//+1位需要缓存的图片名
<span style="white-space:pre"> </span>String filename = path.substring(start + 1);
<span style="white-space:pre"> </span>// 将图片缓存到sd卡上
<span style="white-space:pre"> </span>File file = new File(Environment.getExternalStorageDirectory(),
<span style="white-space:pre"> </span>filename);
<span style="white-space:pre"> </span>if (file.exists() && file.length() > 0) {// 图片已经缓存在根目录
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Log.i(tag, "使用缓存图片");
<span style="white-space:pre"> </span>iv_image.setImageURI(Uri.fromFile(file));
<span style="white-space:pre"> </span>} else {
<span style="white-space:pre"> </span>Log.i(tag, "下载图片并缓存到sd卡");
<span style="white-space:pre"> </span>// Url一般用来代表一个网络资源
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>URL url = new URL(path);
<span style="white-space:pre"> </span>URLConnection urlconn = url.openConnection();
<span style="white-space:pre"> </span>HttpURLConnection conn = (HttpURLConnection) urlconn;
<span style="white-space:pre"> </span>// 设置请求方式为GET
<span style="white-space:pre"> </span>conn.setRequestMethod("GET");
<span style="white-space:pre"> </span>// 设置请求超时时间,5000为5秒钟
<span style="white-space:pre"> </span>conn.setConnectTimeout(5000);
<span style="white-space:pre"> </span>conn.addRequestProperty(
<span style="white-space:pre"> </span>"User-Agent",
<span style="white-space:pre"> </span>" Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)");
<span style="white-space:pre"> </span>// 把GET请求发送出去,获取服务器上的数据
<span style="white-space:pre"> </span>// 获取服务器的状态码
<span style="white-space:pre"> </span>// 200代表ok,404代表资源没找到,五百多代表服务器内部错误,302代表对象已转移
<span style="white-space:pre"> </span>int code = conn.getResponseCode();
<span style="white-space:pre"> </span>if (code == 200) {
<span style="white-space:pre"> </span>// 数据以流的方式发回
<span style="white-space:pre"> </span>InputStream is = conn.getInputStream();
<span style="white-space:pre"> </span>Bitmap bitmap = BitmapFactory.decodeStream(is);
<span style="white-space:pre"> </span>//缓存图片到sd卡上
<span style="white-space:pre"> </span>//第一个参数:图片缓存的格式
<span style="white-space:pre"> </span>//第二个参数:图片缓存的质量,0~100
<span style="white-space:pre"> </span>//第三个参数:图片的输出流
<span style="white-space:pre"> </span>FileOutputStream stream=new FileOutputStream(file);
<span style="white-space:pre"> </span>bitmap.compress(CompressFormat.JPEG, 100, stream);
<span style="white-space:pre"> </span>iv_image.setImageBitmap(bitmap);
<span style="white-space:pre"> </span>} else {
<span style="white-space:pre"> </span>Toast.makeText(this, "请求失败", 1).show();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>} catch (Exception e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>Toast.makeText(this, "访问图片失败", 0).show();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
这样就大功搞成啦,开开心心写代码~oh
----------------------------晴天_1993----------------------------