package com.tang.netview;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
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.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
/**
* @描述: TODO
*
* @作者:
* @时间: 2015-9-7上午1:47:33
*/
public class MainActivity extends Activity
{
private String TAG = "MainActivity";
private static final int YICHANG = 3;
private static final int SUCCESSONE = 1;
private static final int SUCCESSTWO = 2;
private static final int FAILE = 4;
private EditText et_path;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
iv = (ImageView) findViewById(R.id.iv);
}
Handler handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case SUCCESSONE:
Bitmap bitmap1 = (Bitmap) msg.obj;
iv.setImageBitmap(bitmap1);
break;
case SUCCESSTWO:
Bitmap bitmap2 = (Bitmap) msg.obj;
iv.setImageBitmap(bitmap2);
break;
case FAILE:
Toast.makeText(MainActivity.this,
"请求失败", 0).show();
break;
case YICHANG:
Toast.makeText(MainActivity.this,
"发生异常", 0).show();
break;
default:
break;
}
};
};
public void chakan(View view)
{
final String path = et_path.getText().toString().trim();// 图片网上的路径
if (TextUtils.isEmpty(path))
{
Toast.makeText(MainActivity.this, "图片路径不能为空", 0).show();
return;
}
new Thread()
{
public void run()
{
getImage(path);
};
}.start();
}
private void getImage(String path)
{
File file = new File(getCacheDir(), Base64.encodeToString(
path.getBytes(), Base64.DEFAULT));// 保持的文件名
if (file.exists() && file.length() > 0)
{
// 那缓存
Toast.makeText(MainActivity.this, "那缓存", 0).show();
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
Message msg = new Message();
msg.what = SUCCESSONE;
msg.obj = bitmap;
handler.sendMessage(msg);
// iv.setImageBitmap(bitmap);
} else
{
// 网上获取图片
try
{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
int code = conn.getResponseCode();
Log.d(TAG, code + "'");
if (code == 200)
{
InputStream is = conn.getInputStream();// 图片的流
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1)
{
fos.write(buffer, 0, len);
}
is.close();
fos.close();
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
Message msg = new Message();
msg.what = SUCCESSTWO;
msg.obj = bitmap;
handler.sendMessage(msg);
// iv.setImageBitmap(bitmap);
} else
{
Message msg = Message.obtain();
msg.what = FAILE;
handler.sendMessage(msg);
// Toast.makeText(MainActivity.this, "请求失败" , 0).show();
}
}
catch (Exception e)
{
e.printStackTrace();
Message msg = Message.obtain();
msg.what = YICHANG;
handler.sendMessage(msg);
// Toast.makeText(MainActivity.this, "发生异常,请求失败" , 0).show();
}
}
}
}
网络图片查看器
猜你喜欢
转载自huanxiang0220.iteye.com/blog/2242162
今日推荐
周排行