实现输入图片地址浏览图片功能
2015-06-30 10:26
399 查看
1.UI设计;
2..实现功能的MainActivity类:
代码如下:
public class MainActivity extends Activity {
private EditText etUrl;
private ImageView ivImageShow;
public static final int SHOWIMAGE=1;
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);
// ivImageShow.setImageBitmap(bitmap);
Message message=new Message();
message.what=SHOWIMAGE;
message.obj=bitmap;
handler.sendMessage(message);
}
}catch(MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
}
}
3.布局 activity_main.xml 代码如下:
<EditText
android:id="@+id/etImageUrl"
android:layout_width="match_parent"
android:layout_height="108dp"
android:ems="10"
android:text="http://172.22.64.20:8080/test/images/0.jpg" />
<requestFocus />
</EditText>
<Button
android:id="@+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/button_bg"
android:onClick="showImage"
android:text="浏览" />
<ImageView
android:id="@+id/ivImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
2..实现功能的MainActivity类:
代码如下:
public class MainActivity extends Activity {
private EditText etUrl;
private ImageView ivImageShow;
public static final int SHOWIMAGE=1;
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);
// ivImageShow.setImageBitmap(bitmap);
Message message=new Message();
message.what=SHOWIMAGE;
message.obj=bitmap;
handler.sendMessage(message);
}
}catch(MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
}
}
3.布局 activity_main.xml 代码如下:
<EditText
android:id="@+id/etImageUrl"
android:layout_width="match_parent"
android:layout_height="108dp"
android:ems="10"
android:text="http://172.22.64.20:8080/test/images/0.jpg" />
<requestFocus />
</EditText>
<Button
android:id="@+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/button_bg"
android:onClick="showImage"
android:text="浏览" />
<ImageView
android:id="@+id/ivImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
相关文章推荐
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- 一个简单的asp数据库操作类
- C#实现用于操作wav声音文件的类实例
- 图片引发的溢出危机(图)
- 在线管理数据库 类
- Lua面向对象之类和继承浅析
- Lua中类的实现原理探讨(Lua中实现类的方法)
- Lua中的类编程代码实例
- C#常用目录文件操作类实例
- C#实现把彩色图片灰度化代码分享
- c# 类和成员的修饰详细介绍
- C#中实现判断某个类是否实现了某个接口
- C#类的创建与初始化实例解析
- C#基础语法:结构和类区别详解
- 深入c# 类和结构的区别总结详解
- c#结构和类的相关介绍
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- C#中类与结构的区别实例分析