关于android的网络访问——实现网络图片查看器
2014-07-25 15:21
429 查看
代码很简单,知识实现一个简单的网络图片查看器的功能,具体实现过程的照片我就不贴了,具体实现的代码如下:
一。布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/imagePath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="http://www.faxingw.cn/userimg/201110/30147.jpg">
</EditText>
<Button
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看图片" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
二。功能实现代码:
package org.jrue.netImageCehecker.activity;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
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.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private EditText imagePath;
private Button btn_check;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inti();
}
private void inti() {
imagePath = (EditText)findViewById(R.id.imagePath);
btn_check = (Button)findViewById(R.id.check);
iv = (ImageView)findViewById(R.id.imageView);
btn_check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(runnable).start();
}
});
}
private Runnable runnable = new Runnable(){
@Override
public void run() {
String path = imagePath.getText().toString().trim();
byte[] data = null;
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
InputStream is = conn.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer))!= -1){
outputStream.write(buffer, 0, len);
}
outputStream.close();
data = outputStream.toByteArray();
}
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Looper.prepare();
Message msg = new Message();
msg.what = 0;
msg.obj = bitmap;
handler.sendMessage(msg);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
if(msg.what == 0){
iv.setImageBitmap((Bitmap) msg.obj);
}
};
};
}
很简单,而且代码放在一起比较冗杂,感兴趣的可以细分下来,定义service包,utils包之类的,以实现代码清晰化。这是很好的编码风格。这里只为展示需要,并没有做多大的处理。
一。布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/imagePath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="http://www.faxingw.cn/userimg/201110/30147.jpg">
</EditText>
<Button
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看图片" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
二。功能实现代码:
package org.jrue.netImageCehecker.activity;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
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.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private EditText imagePath;
private Button btn_check;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inti();
}
private void inti() {
imagePath = (EditText)findViewById(R.id.imagePath);
btn_check = (Button)findViewById(R.id.check);
iv = (ImageView)findViewById(R.id.imageView);
btn_check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(runnable).start();
}
});
}
private Runnable runnable = new Runnable(){
@Override
public void run() {
String path = imagePath.getText().toString().trim();
byte[] data = null;
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
InputStream is = conn.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer))!= -1){
outputStream.write(buffer, 0, len);
}
outputStream.close();
data = outputStream.toByteArray();
}
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Looper.prepare();
Message msg = new Message();
msg.what = 0;
msg.obj = bitmap;
handler.sendMessage(msg);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
if(msg.what == 0){
iv.setImageBitmap((Bitmap) msg.obj);
}
};
};
}
很简单,而且代码放在一起比较冗杂,感兴趣的可以细分下来,定义service包,utils包之类的,以实现代码清晰化。这是很好的编码风格。这里只为展示需要,并没有做多大的处理。
相关文章推荐
- Android编程实现网络图片查看器和网页源码查看器实例
- Android实现网络图片查看器和网页源码查看器
- android网络图片查看器简单实现代码
- Android实现网络图片查看器和网页源码查看器
- Android实现网络图片查看器和网页源码查看器
- android---利用android-async-http开源项目实现网络图片查看器
- Android实现网络图片查看器和网页源码查看器
- Android实现网络图片查看器和网页源码查看器
- Android实现网络图片查看器和网页源码查看器
- Android实现网络图片查看器
- Android 实现网络访问图片
- android学习笔记16--------------Android实现网络图片与html源码获取
- Android学习--网络通信之网络图片查看器
- Android中AsyncTask的简单用法及实现网络图片下载的实例
- 在android使用mina实现网络访问
- Android编程之从网络上获取图片查看器和HTML代码的查看器
- Android中用BaseAdapter实现带Button和显示网络图片的Listview
- Project——从网络获取数据,实现图片查看器
- android Gallery实现异步加载网络图片 并只加载当前停止页面图
- Android 网络显示图片 通过代理访问