android 查看网络图片,android基础--网络图片查看器

package cn.com.netimage;

public class MainActivity extends Activity {

private EditText imagepath;

private ImageView imageView;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

imagepath = (EditText) this.findViewById(R.id.imagepath);

imageView = (ImageView) this.findViewById(R.id.imageView);

//找到按钮

Button button = (Button) this.findViewById(R.id.button);

//为按钮增加点击事件

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

//得到路径

String path = imagepath.getText().toString();

try {

//获取图片数据

byte [] data = ImageService.getImage(path);

if(data !=null){

Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

//显示图片

imageView.setImageBitmap(bitmap);

}else{

Toast.makeText(getApplicationContext(), R.string.error, 1).show();

}

} catch (Exception e) {

Toast.makeText(getApplicationContext(), R.string.error, 1).show();

}

}

});

}

}

package cn.com.service;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import cn.com.utils.StreamTool;

public class ImageService {

/**

* 获取图片

* @param path 网络图片路径

* @return 图片的字节数据

*/

public static byte[] getImage(String path)throws Exception {

//path包装成URL对象

URL url = new URL(path);

//打开基于http协议的链接

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

//设置链接超时时间

connection.setConnectTimeout(5000);

//设置请求方式

connection.setRequestMethod("GET");

//得到输入流

if(connection.getResponseCode()==200){//请求成功

InputStream inStream = connection.getInputStream();

//做一个工具类来读取

byte[] data = StreamTool.read(inStream);

return data;

}

return null;

}

}

package cn.com.utils;

public class StreamTool {

/**

* 读取输入流数据

* @param inStream

* @return

* @throws IOException

*/

public static byte[] read(InputStream inStream) throws IOException {

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

byte [] buffer = new byte[1024];

int len = 0;

while((len=inStream.read(buffer))!=-1){

outputStream .write(buffer, 0, len);

}

inStream.close();

return outputStream.toByteArray();

}

}

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/app_name"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="http://192.168.1.131:8080/videonews/a.jpg"

android:id="@+id/imagepath"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/button"

android:id="@+id/button"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/imageView"

/>

设置访问权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值