Android编程获取图片数据的方法详解

 更新时间:2017年07月19日 09:36:28   作者:青蛙小王子  
这篇文章主要介绍了Android编程获取图片数据的方法,涉及Android网络通信数据流传输及图片操作相关技巧,需要的朋友可以参考下

本文实例讲述了Android编程获取图片数据的方法。分享给大家供大家参考,具体如下:

网络的访问在我们日常生活中太重要了,如果没有网络我们的生活将会是什么样子呢?Android手机和浏览器也是一样的,也可以通过网络通讯获取数据,如调用webservice,EJB等。下面就通过一个小例子从网络获取一幅图片并显示在手机上,开发中将会使用到一个新的组件ImageView.

1. 写一个用来处理字节流的工具类

package org.lxh.util;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamTool {
  public static byte[] readInputStream(InputStream in) throws Exception{
    int len=0;
    byte buf[]=new byte[1024];
    ByteArrayOutputStream out=new ByteArrayOutputStream();
    while((len=in.read(buf))!=-1){
      out.write(buf,0,len); //把数据写入内存
    }
    out.close(); //关闭内存输出流
    return out.toByteArray(); //把内存输出流转换成byte数组
  }
}

2. 写一个得到图片byte数组的service类

package org.lxh.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.lxh.util.StreamTool;
import android.util.Log;
public class WebService {
  public static byte[] getImage(String path){
    URL url;
    byte[] b=null;
    try {
      url = new URL(path);  //设置URL
      HttpURLConnection con;
      con = (HttpURLConnection)url.openConnection(); //打开连接
      con.setRequestMethod("GET"); //设置请求方法
      //设置连接超时时间为5s
      con.setConnectTimeout(5000);
      InputStream in=con.getInputStream(); //取得字节输入流
      b=StreamTool.readInputStream(in);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return b; //返回byte数组
  }
}

3. 写一个用户操作界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
  <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/picaddress"
  />
  <EditText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="http://www.desk9.com/Desk9Image/21/Desk9_21_1690_35790_S.jpg"
  android:id="@+id/imageaddress"
  />
  <Button
   android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/look"
  android:id="@+id/button"
  />
  <ImageView
   android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/image"/>
</LinearLayout>

4. 写一个activity类

package org.lxh.net;
import org.lxh.service.WebService;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class NetActivity extends Activity {
  private EditText picaddress;
  private Button button;
  private ImageView imageView;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button=(Button)this.findViewById(R.id.button);
    imageView=(ImageView)this.findViewById(R.id.image);
    picaddress=(EditText)this.findViewById(R.id.imageaddress);
    button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        String address=picaddress.getText().toString();
        try {
          byte[] data=WebService.getImage(address); //得到图片的输入流
          //二进制数据生成位图
          Bitmap bit=BitmapFactory.decodeByteArray(data, 0, data.length);
          imageView.setImageBitmap(bit);
        } catch (Exception e) {
          Log.e("NetActivity", e.toString());
          Toast.makeText(NetActivity.this, R.string.error, 1).show();
        }
      }
    });
  }
}

5. 添加网络访问的权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="org.lxh.net"
   android:versionCode="1"
   android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".NetActivity"
         android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
  <uses-sdk android:minSdkVersion="7" />
  <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

6. 这里把strings.xml文件也贴出来

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Hello World, NetActivity!</string>
  <string name="app_name">图片查看</string>
  <string name="picaddress">图片地址</string>
  <string name="look">查看</string>
  <string name="error">网络连接异常</string>
</resources>

下面是运行效果图:

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android入门之Fragment的使用教程

    Android入门之Fragment的使用教程

    Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑。本文主要来和大家讲讲Fragment的使用,感兴趣的小伙伴可以了解一下
    2022-12-12
  • Android Notification使用教程详解

    Android Notification使用教程详解

    这篇文章主要介绍了Android Notification使用,通知的使用的内容还是比较多的,此篇文章将会尽可能详细的介绍Notification的内容,需要的朋友可以参考下
    2022-07-07
  • Android学习笔记之Shared Preference

    Android学习笔记之Shared Preference

    在之前遇到有个需求是要改settings里面自动转屏的首选项,于是就学习了下Shared Preference。Shared Preference是一种简单的、轻量级的键/值对机制,用于保存原始应用程序数据,最常见的就是首选项
    2013-09-09
  • Android实现多线程断点续传

    Android实现多线程断点续传

    这篇文章主要为大家详细介绍了Android实现多线程断点续传,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • android实现弹出提示框

    android实现弹出提示框

    这篇文章主要为大家详细介绍了android实现弹出提示框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • 在Android模拟器上模拟GPS功能总是null的解决方法

    在Android模拟器上模拟GPS功能总是null的解决方法

    在我们开发时需要在模拟器上模拟GPS,可在Location的时候总是null,下面与大家分享下具体的解决方法,感兴趣的朋友可以参考下哈
    2013-06-06
  • Android 边播边缓存的实现(MP4 未加密m3u8)

    Android 边播边缓存的实现(MP4 未加密m3u8)

    这篇文章主要介绍了Android 边播边缓存的实现(MP4 未加密m3u8),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Jenkins打包android应用时自动签名apk详解

    Jenkins打包android应用时自动签名apk详解

    这篇文章主要介绍了Jenkins打包android应用时自动签名apk详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • Flutter简洁实用的图片编辑器的实现

    Flutter简洁实用的图片编辑器的实现

    本文主要介绍了Flutter简洁实用的图片编辑器的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Android 代码一键实现银行卡绑定功能

    Android 代码一键实现银行卡绑定功能

    这篇文章主要介绍了Android 代码一键实现银行卡绑定功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04

最新评论