Android图片查看器

时间:2023-01-27 18:51:18

转载请标明出处:http://blog.csdn.net/wu_wxc/article/details/53705885
本文出自【吴孝城的CSDN博客】

Android图片查看器
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.wuxiaocheng.pictureviewer.MainActivity">

<EditText
android:id="@+id/et_imgurl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入图片url" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="查看" />

<ImageView
android:id="@+id/img_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />

</LinearLayout>

MainActivity.java

package cn.wuxiaocheng.pictureviewer;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private EditText etImageUrl;
private ImageView imageView;
private static final int MSG_CACHE_PIC = 1;
private static final int MSG_NEW_PIC = 2;
private static final int ERROR = 3;
private static final int EXCEPTION = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etImageUrl = (EditText) findViewById(R.id.et_imgurl);
imageView = (ImageView) findViewById(R.id.img_view);
}
public void click(View v) {
final String url = etImageUrl.getText().toString();
// 用TextUtils.isEmpty()判断是否为空
if (TextUtils.isEmpty(url)) {
Toast.makeText(MainActivity.this, "图片路径不能为空", Toast.LENGTH_SHORT).show();
return;
}
new Thread() {
@Override
public void run() {
getImage(url);
}
}.start();
}
private void getImage(String url) {
// getCacheDir()会将图片放在data/data/包名/cache下
File file = new File(getCacheDir(), Base64.encodeToString(url.getBytes(), Base64.DEFAULT));
// 如果图片存在
if (file.exists() && file.length() > 0) {
System.out.println("文件存在,拿缓存");
// decodeFile可以将手机系统里的图片文件转换成Bitmap对象
// getAbsolutePath()拿到文件的绝对路径
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
// 声明消息,更新UI
Message msg = new Message();
msg.what = MSG_CACHE_PIC;
// 设置参数
msg.obj = bitmap;
handler.sendMessage(msg);
} else {
// 图片不存在获取图片生成缓存
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
int code = conn.getResponseCode();
if (code == 200) {
// png的图片
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
// 写入图片字节
fos.write(buffer, 0, len);
}
is.close();
fos.close();
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
// 重写UI
Message msg = new Message();
msg.what = MSG_NEW_PIC;
msg.obj = bitmap;
handler.sendMessage(msg);
} else {
//请求失败
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
// 发生异常,请求失败
Message msg = new Message();
msg.what = EXCEPTION;
handler.sendMessage(msg);
}
}
}
// 在主线程中声明消息处理机制handler
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_CACHE_PIC:
Bitmap bitmap = (Bitmap) msg.obj;
imageView.setImageBitmap(bitmap);
System.out.println("缓存的图片");
break;
case MSG_NEW_PIC:
Bitmap bitmap1 = (Bitmap) msg.obj;
imageView.setImageBitmap(bitmap1);
System.out.println("下载图片");
break;
case ERROR:
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
break;
case EXCEPTION:
Toast.makeText(MainActivity.this, "发生异常", Toast.LENGTH_SHORT).show();
break;
}
}
};
}

Android图片查看器