网络图片查看器HttpURLConnection+Bitmap
来源:互联网 发布:淘宝代购点怎么做 编辑:程序博客网 时间:2024/06/18 18:09
网络图片查看器
软件界面
- 网络图片查看器
- 软件界面
- MainActivityjava
- activity_mainxml
- 需加权限
MainActivity.java
package com.peng.image_show;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;public class MainActivity extends AppCompatActivity { private ImageView iv; private EditText et_path; /*private Handler handle = new Handler(){ @Override public void handleMessage(Message msg) { Bitmap bitmap = (Bitmap) msg.obj; iv.setImageBitmap(bitmap); } };*/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); } public void click(View view) { new Thread() { public void run() { try { //获取图片路径 String path = et_path.getText().toString().trim(); //File file = new File(getCacheDir(), Base64.encodeToString(path.getBytes(),Base64.DEFAULT); File file = new File(getCacheDir(), "test.png"); if (file.exists() && file.length() > 0) { //使用缓存图片 System.out.println("---->>>>>>>>>>使用缓存的图片"); final Bitmap cacheBitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); /* Message msg = Message.obtain(); msg.obj = cacheBitmap; handle.sendMessage(msg);*/ runOnUiThread(new Runnable() { @Override public void run() { iv.setImageBitmap(cacheBitmap); } }); } else { System.out.println("---->>>>>>>>>>使用网络的图片"); //第一次访问连接网络 //创建url对象 URL url = new URL(path); //获取httpurlconnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //连接方式 conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); //获取服务器的状态码 int code = conn.getResponseCode(); if (code == 200) { //获取图片的数据,不管什么数据,都是流的形式返回 InputStream in = conn.getInputStream(); file = new File(getCacheDir(), "test.png"); FileOutputStream fos = new FileOutputStream(file); //fos文件输出流 int len = -1; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) != -1) { fos.write(buffer, 0, len); } fos.close(); in.close(); //获取位图工厂,获取bitmap final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); runOnUiThread(new Runnable() { @Override public void run() { //run方法一定执行在UI线里 iv.setImageBitmap(bitmap); } }); /*Message msg =Message.obtain();//msg的静态方法,减少对象的创建 msg.obj =bitmap; handle.sendMessage(msg);//发消息*/ } } } catch (Exception e) { e.printStackTrace(); } } }.start(); }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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="com.peng.image_show.MainActivity"> <EditText android:id="@+id/et_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入查看的网址" android:text="http://www.pptbz.com/pptpic/UploadFiles_6909/201204/2012041411433867.jpg" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="查看" /> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
需加权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
0 0
- 网络图片查看器HttpURLConnection+Bitmap
- android 简单网络图片查看器 HttpURLConnection
- 4.4网络图片查看器HttpURLConnection
- Androidx学习笔记(28)--- 网络图片查看器---HttpURLConnection
- Android图片查看器(Bitmap)
- 【HttpUrlConnection】网页图片查看器(2)
- android HttpURLConnection编程详解,获取网络图片,html代码查看
- 使用HttpURLConnection查看图片
- HttpURLConnection下载网络图片
- HttpURLConnection获取网络图片
- HttpURLConnection下载网络图片
- android开发-通过HttpURLConnection获取网络内容,Bitmap把二进制格式的图片转为位图
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- *******************网络图片查看器*************
- 删除mysql binlog
- What is EPOLL? Epoll vs Poll vs Select call ? And How to implement UDP server in Linux using EPOLL?
- this
- Codeforces Round #334 (Div. 2)
- OPC 目标平台只能是 X86吗?--尝试读取或写入受保护的内存
- 网络图片查看器HttpURLConnection+Bitmap
- windows安装mq
- 解决fragment中嵌套viewpager,vierpager中有多个fragment,不显示的问题
- C#socket建立服务器并进行简单通信
- __toString()方法
- C#初学者json解析
- 数据结构实验之二叉树六:哈夫曼编码
- Android 内存优化 (防Memory Leak)
- android开发步步为营之110:android反编译apk