十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Android实现图片查看器代码如下:
为嘉禾等地区用户提供了全套网页设计制作服务,及嘉禾网站建设行业解决方案。主营业务为成都网站制作、成都网站建设、嘉禾网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
public class MainActivity extends Activity implements OnClickListener{
private EditText et_url;
private button btn_show;
private ImageView iv_pic;
/**
* 成功获取数据
*/
private final int GET_DATA_SUCESS = 1;
private final int NETWORK_ERROR = 2;
private final int SERVER_ERROR = 3;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case GET_DATA_SUCESS:
bitmap bm = (bitmap) msg.obj;
iv_pic.setImagebitmap(bm);
Toast.makeText(MainActivity.this, "从网络获取数据", Toast.LENGTH_SHORT).show();
break;
case NETWORK_ERROR:
Toast.makeText(MainActivity.this, "网络连接异常", Toast.LENGTH_SHORT).show();
break;
case SERVER_ERROR:
Toast.makeText(MainActivity.this, "服务器返回异常", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
};
};
@Override
protected void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_url = (EditText) findViewbyId(R.id.et_url);
btn_show = (button) findViewbyId(R.id.btn_show);
iv_pic = (ImageView) findViewbyId(R.id.iv_pic);
src="/upload/article1/tomcat1.png");
btn_show.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//
final File file = new src="/upload/article1/");
if(file != null&&file.length()>0){
bitmap bm = bitmapFactory.decodeFile(file.getAbsolutepath());
iv_pic.setImagebitmap(bm);
Toast.makeText(this, "使用缓存", Toast.LENGTH_SHORT).show();
return;
}
new Thread(){
public void run() {
//①获取网址
String path = et_url.getText().toString().trim();
//②联网
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
//设置连接超时时间
connection.setConnectTimeout(10000);
int code = connection.getResponseCode();
if(code==200){
//获取流
InputStream inputStream = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
int len = -1;
byte buffer[] = new byte[1024];
while((len = inputStream.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
fos.close();
inputStream.close();
//通过流创建一个bitmap对象
//bitmap bitmap = bitmapFactory.decodeStream(inputStream);
bitmap bitmap = bitmapFactory.decodeFile(file.getAbsolutepath());
//iv_pic.setim
//创建消息
Message msg = Message.obtain();
//通过消息携带bitmap在主线程中展示
msg.what = GET_DATA_SUCESS;
msg.obj = bitmap;
//通过handler发送消息给主线程
handler.sendMessage(msg);
}else{
Message msg = Message.obtain();
msg.what = SERVER_ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = NETWORK_ERROR;
handler.sendMessage(msg);
}
//③获取图片
//展示图片
};
}.start();
}
}
以上代码包含了查看图片以及下载图片,而下面的java代码主要是查看图片,不是下载图片。
public class MainActivity extends AppCompatActivity {
private EditText et_url;
private button btn_show;
private ImageView iv_pic;
private final int GET_DATA_SUCESS = 1;
private final int NETWORK_ERROR = 2;
private final int SERVER_ERROR = 3;
private Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what) {
case GET_DATA_SUCESS:
bitmap bm = (bitmap) msg.obj;
iv_pic.setImagebitmap(bm);
Toast.makeText(MainActivity.this, "图片是", Toast.LENGTH_SHORT).show();
break;
case NETWORK_ERROR:
Toast.makeText(MainActivity.this, "网址异常", Toast.LENGTH_SHORT).show();
break;
case SERVER_ERROR:
Toast.makeText(MainActivity.this, "服务器错误", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
@Override
protected void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_url = (EditText) findViewbyId(R.id.et_url);
btn_show = (button) findViewbyId(R.id.btn_show);
iv_pic = (ImageView) findViewbyId(R.id.iv_pic);
src="/upload/article1/index_banner4.jpg");
btn_show.setOnClickListener(new MyonClicklistener());
}
private class MyonClicklistener implements View.OnClickListener {
@Override
public void onClick(View v){
new Thread(){
public void run(){
String path = et_url.getText().toString().trim();
URL url = null;
try {
url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
int code = connection.getResponseCode();
if(code==200){
InputStream inputStream = connection.getInputStream();
bitmap bitmap = bitmapFactory.decodeStream(inputStream);
Message msg = new Message();
msg.what = GET_DATA_SUCESS;
msg.obj=bitmap;
handler.sendMessage(msg);
//tv_code.setText(result);
}else{
Message msg = Message.obtain();
msg.what = SERVER_ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = NETWORK_ERROR;
handler.sendMessage(msg);
}
}
}.start();
}
}
}