读取网路图片
16lz
2021-01-26
1.MainActivity
package com.example.httpimagedemo;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 MainActivity extends Activity {private static final String PATH = "http://img1.imgtn.bdimg.com/it/u=3199407981,256809178&fm=23&gp=0.jpg";private ImageView img = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);img = (ImageView) findViewById(R.id.img);new Thread(new mThread()).start();}private class mThread implements Runnable {@Overridepublic void run() {try {byte data[] = getUrlData();Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); // 生成图片img.setImageBitmap(bm);} catch (Exception e) {e.printStackTrace();}}}public byte[] getUrlData() throws Exception {ByteArrayOutputStream bos = null; // 内存输出流try {URL url = new URL(PATH);bos = new ByteArrayOutputStream();byte data[] = new byte[1024]; // 每次读取1024HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 打开InputStream input = conn.getInputStream(); // 取得输入流int len = 0; // 读取的长度while ((len = input.read(data)) != -1) { // 如果没有的读取到底部(如果是底部的话,会返回-1),则继续读取bos.write(data, 0, len); // 向内存中保存}return bos.toByteArray();// 变为字节数组返回} catch (Exception e) {throw e;} finally {bos.close();// 关闭输入流}}}
2.activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
3.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.httpimagedemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.httpimagedemo.MainActivity" 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></manifest>
更多相关文章
- Android(安卓)Studio入门小例子
- Android获取CPU,内存等系统信息方法
- 输入处理 Input Processor
- android ZRAM
- Build Android(安卓)Studio development environment in Linux
- android 4中文件操作
- Android中SharedPreference多进程数据共享出错
- android 多个cursor 的读取 和读取某个Cursor其他从集合中获取的
- android 客制化系统签名文件