现在的位置: 首页 > 综合 > 正文

读取网络图片

2018年03月31日 ⁄ 综合 ⁄ 共 2664字 ⁄ 字号 评论关闭

 

 

 

在main.xml中:

 

<?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"

  android:background="#3399ff">

  <ImageView

     android:id="@+id/img"

     android:gravity="center_horizontal"

     android:layout_marginTop="20dp"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content"/>

</LinearLayout>

 

 

 

 

 

在MyWebDemo.java中:

 

package com.e.webproject;

 

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

 

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.widget.ImageView;

 

public class MyWebDemo extends Activity {

  private ImageView img = null;

private static final String PATH = "http://www.diyifanwen.com/images/guangxi/

0872402543275431.jpg"; // 请求地址

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.img = (ImageView) super.findViewById(R.id.img);

     try {

       byte data [] = this.getUrlData() ;

       Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); // 把二进制变为图片

       this.img.setImageBitmap(bm) ;

     } catch (Exception e) {

     }

  }

  public byte[] getUrlData() throws Exception {  // 通过此操作读取指定地址上的信息

     ByteArrayOutputStream bos = null ; // 内存操作流完成

     try {

       URL url = new URL(PATH) ;

       bos = new ByteArrayOutputStream() ;

       byte data [] = new byte[1024] ;

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

       InputStream input = conn.getInputStream() ;

       int len = 0 ;

       while ((len = input.read(data)) != -1) {

         bos.write(data, 0, len);

       }

       return bos.toByteArray() ;

     } catch (Exception e) {

       throw e ;

     } finally {

       if(bos != null) {

         bos.close() ;

       }

     }

  }

}

 

 

 

 

 

在AndroidManifest.xml中修改权限:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.e.webproject"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

  <uses-permission android:name="android.permission.INTERNET" />

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyWebDemo"

            android:label="@string/title_activity_my_web_demo" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

抱歉!评论已关闭.