网络图片查看器
来源:互联网 发布:灰原哀毛利兰人气知乎 编辑:程序博客网 时间:2024/05/17 03:30
activity_main.xml代码如下:
共运用了三个控件:ImageView EditText Button
<ImageViewandroid:id="@+id/ivImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
<EditText
android:id="@+id/etImageUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btnView"
android:layout_alignParentRight="true"
android:ems="10"
android:text=""
android:hint="请输入图片地址" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="46dp"
android:layout_gravity="center"
android:background="@drawable/b"
android:onClick="viewImage"
android:text="浏览" />
</RelativeLayout>
MainActivity.java代码
package bzu.edu.cn.interentimageview1;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Handler;
import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView ivImageShow;
private EditText etUrl;
public static final int SHOWIMAGE=1;
protected static final Context NetImageBrouserActivity = null;
//在主线程中创建消息处理器
private Handler handler=new Handler(){
public void handleMessage(Message msg){
//对接收到的新的消息进行处理
switch (msg.what) {
case SHOWIMAGE:
Bitmap bitmap=(Bitmap)msg.obj;
ivImageShow.setImageBitmap(bitmap);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
etUrl=(EditText)findViewById(R.id.etImageUrl);
ivImageShow=(ImageView)findViewById(R.id.ivImage);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//访问网络,从网络中获取图片
public void showImage(View view){
final String path=etUrl.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 connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
int responseCode=connection.getResponseCode();
if(responseCode==200){
InputStream inputStream=connection.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
Message message=new Message();
message.what=SHOWIMAGE;
message.obj=bitmap;
handler.sendMessage(message);
}
//else{
//Toast.makeText(NetImageBrouserActivity.this, "显示图片失败!",Toast.LENGTH_SHORT).show();
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
};
}.start();
}
}
}
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- *******************网络图片查看器*************
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 1076. Trash(KM算法 二分最佳完美匹配)
- 【poj 2533】Longest Ordered Subsequence
- Leetcode--easy系列10
- programming-challenges Stacks of Flapjacks (110402) 题解
- 14个Xcode中常用的快捷键操作
- 网络图片查看器
- 黑马程序员_String
- 微软100题
- FaceRecognizer
- 面试题总结4
- openstack nova 基础知识——policy【转载】
- 【无中生有】---11---服务器集群部署
- PHP100-6 编写一个函数解析多重嵌套数组的所有元素
- 设计模式--浅谈工厂方法模式