Android代码(Handler的运用),HttpURLConnection的应用,将url图片地址转换成图片。

??

1 布局文件,


<LinearLayout 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:orientation="vertical"

tools:context=".MainActivity" >

<ImageView

android:id="@+id/iv_icon"

android:layout_width="fill_parent"

android:layout_height="0dip"

android:layout_weight="1" />

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<EditText

android:id="@+id/et_url"

android:layout_width="0dip"

android:text="http://img0.hao123.com/data/1_489a5f7dfbcbb624a231f372e4bdffce_310"

android:layout_height="wrap_content"

android:singleLine="true"

android:layout_weight="1" />

<Button

android:id="@+id/btn_submit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Go"

            android:textSize="20sp" />

</LinearLayout>

</LinearLayout>


package com.itheim28.submitdata;

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.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private static final String TAG = "MainActivity";

protected static final int ERROR = 1;

private EditText etUrl;

private ImageView ivIcon;

private final int SUCCESS = 0;

private Handler handler = new Handler() {

public void handleMessage(Message msg) {

super.handleMessage(msg);

Log.i(TAG, "what = " + msg.what);

if (msg.what == SUCCESS) {

ivIcon.setImageBitmap((Bitmap)msg.obj);

} else if (msg.what == ERROR) {

Toast.makeText(MainActivity.this, "抓起失败", 0).show();

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ivIcon = (ImageView) findViewById(R.id.iv_icon);

etUrl = (EditText) findViewById(R.id.et_url);

findViewById(R.id.btn_submit).setOnClickListener(this);

}

@Override

public void onClick(View v) {

final String url = etUrl.getText().toString();

new Thread(new Runnable() {

@Override

public void run() {

Bitmap bitmap = getImageFromNet(url);

//            ivIcon.setImageBitmap(bitmap);     // 设置imageView显示的图片

if(bitmap != null) {

Message msg = new Message();

msg.what = SUCCESS;

msg.obj = bitmap;

handler.sendMessage(msg);

} else {

Message msg = new Message();

msg.what = ERROR;

handler.sendMessage(msg);

}

}}).start();

}

/**

* 根据url连接取网络抓去图片返回

* @param url

* @return url对应的图片

*/

private Bitmap getImageFromNet(String url) {

HttpURLConnection conn = null;

try {

URL mURL = new URL(url); // 创建一个url对象

// 得到http的连接对象

conn = (HttpURLConnection) mURL.openConnection();

conn.setRequestMethod("GET");      // 设置请求方法为Get

conn.setConnectTimeout(10000);     // 设置连接服务器的超时时间, 如果超过10秒钟, 没有连接成功, 会抛异常

conn.setReadTimeout(5000);      // 设置读取数据时超时时间, 如果超过5秒, 抛异常

conn.connect();      // 开始链接

int responseCode = conn.getResponseCode(); // 得到服务器的响应码

if(responseCode == 200) {

// 访问成功

InputStream is = conn.getInputStream();   // 获得服务器返回的流数据

Bitmap bitmap = BitmapFactory.decodeStream(is); // 根据 流数据 创建一个bitmap位图对象

return bitmap;

} else {

Log.i(TAG, "访问失败: responseCode = " + responseCode);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if(conn != null) {

conn.disconnect();       // 断开连接

}

}

return null;

}

}

时间: 2025-01-15 14:35:11

Android代码(Handler的运用),HttpURLConnection的应用,将url图片地址转换成图片。的相关文章

Android View转换成图片保存

package zhangphil.viewtoimage; import java.io.File; import java.io.FileOutputStream; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.Tex

C# 字节流通过Base64编码转换成图片代码

C# 字节流通过Base64编码转换成图片代码 // 需载入以下的命名空间 // using System.IO; // using System.Drawing; // using System.Runtime.Serialization.Formatters.Binary; protected void Page_Load(object sender, EventArgs e) { byte[] buffer = ReadFile(Server.MapPath(@"\sex.txt"

c语言代码编程题汇总:将字符串中的大写字母转换成小写字母

将字符串中的大写字母转换成小写字母 程序代码如下: 1 /* 2 2017年3月8日21:21:46 3 功能:将字符串中的大写字母转换成小写字母 4 */ 5 /* 6 #include"stdio.h" 7 8 int main() 9 { 10 int n = 0; 11 12 char a[100]; 13 14 printf("please input a string:"); 15 16 gets(a); 17 18 for(int i = 0 ;a[i

Android图片圆角转换 RoundedImageView开源项目 小记

Android 将图片快速转换成圆角的方法 使用开源项目  RoundedImageView github上面的开源项目 官方地址为:   https://github.com/vinc3m1/RoundedImageView 效果如下:     下面快速的集成进来 步骤分为3个 1: 去github上下载 工程 https://github.com/vinc3m1/RoundedImageView 2: 导入工程 3  在布局中使用它 <com.makeramen.rounded.Rounde

Android的Handler机制

Handler机制的原理 Android 的 Handler 机制(也有人叫消息机制)目的是为了跨线程通信,也就是多线程通信.之所以需 要跨线程通信是因为在 Android 中主线程通常只负责 UI 的创建和修改,子线程负责网络访问和耗时操作, 因此,主线程和子线程需要经常配合使用才能完成整个 Android 功能. Handler 机制可以近似用图 1 展示.MainThread 代表主线程,newThread 代表子线程. MainThread 是 Android 系统创建并维护的,创建的时

常用Android代码

这里收集了大家常用的一些Android代码,持续更新中,内容来自自己的平时积累和网络上看到的文章,部分原文地址在最下方.如有错误欢迎指正里面可能会有重复内容,请忽略或者提醒我删除. setBackgroundResource(0) 可以移除 View 的背景色 Resources.getSystem().getDisplayMetrics().density 可以不用 Context 也能获取屏幕密度哦 通过重载 ViewGroup 的 dispatchDraw 可以实现一个简单的蒙版效果. 例

Android的Handler深入解析

1.概述 前面写过一篇文章<Android中的消息机制>简单分析了异步消息机制,本文将深入解读Handler的原理. 2.基本概念 单线程模型中的Message.Handler.Message Queue.Looper之间的关系: Handler获取当前线程的Looper对象,Looper用来从存放Message的MessageQueue中取出Message,再由Handler进行Message的分发和处理. (1)Message Queue(消息队列) 用来存放通过Handler发布的消息,

Android Thread Handler UIHandler demos

extends:http://blog.csdn.net/superjunjin/article/details/7540064 序效果:为了显示如何用message传值的简单例子 例1,点击按钮,持续显示当前系统时间(bundle传值,耗时,效率低) 例2,点击按钮,progressbar持续前进(message方法传值,效率高,但只能传整型int和对象object) 例1,主activity  package com.song; import java.text.SimpleDateForm

Android 使用handler实现线程间发送消息 (主线程 与 子线程之间)、(子线程 与 子线程之间)

keyword:Android 使用handler实现线程间发送消息 (主线程 与 子线程之间).(子线程 与 子线程之间) 相信大家平时都有使用到异步线程往主线程(UI线程)发送消息的情况. 本文主要研究Handler的消息发送. 包含主线程往子线程发送消息,子线程之间互相发送消息. 一.主线程向子线程发送消息. 实现过程比較简单: 主线程发送消息到异步线程.异步线程接收到消息后在再发送一条消息给主线程. 1. 初始化主线程的Handler,用来接收子线程的消息. 2. 启动异步线程.在异步线