一.web网络图片查看器Android

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><ImageViewandroid:id="@+id/iv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="8dp"android:layout_marginLeft="8dp"android:layout_marginTop="8dp"android:layout_marginEnd="8dp"android:layout_marginRight="8dp"android:layout_marginBottom="8dp"android:scaleType="center"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:srcCompat="@mipmap/ic_launcher" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:layout_marginLeft="16dp"android:layout_marginBottom="56dp"android:onClick="pre"android:text="上一张"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="16dp"android:layout_marginRight="16dp"android:layout_marginBottom="56dp"android:onClick="next"android:text="下一张"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

MainActivity

package com.glsite.netimageviewer;import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private static final int LOAD_ERROR = 2;private static final int LOAD_IMAGE = 1;private ImageView mIv;private Handler mHandler = new Handler(new Handler.Callback() {@Overridepublic boolean handleMessage(Message msg) {switch (msg.what) {case LOAD_IMAGE:Bitmap bitmap = (Bitmap) msg.obj;mIv.setImageBitmap(bitmap);Toast.makeText(MainActivity.this, "加载图片成功", Toast.LENGTH_SHORT).show();break;case LOAD_ERROR:System.out.println("LOAD_ERROR");Toast.makeText(MainActivity.this, "加载图片失败", Toast.LENGTH_SHORT).show();break;default:break;}return false;}});private ArrayList<String> mPaths;private int currentPosition = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mIv = findViewById(R.id.iv);// 1.连接服务器,获取所有的图片链接信息loadAllImagePath();}/*** 获取全部图片资源路径*/private void loadAllImagePath() {new Thread() {@Overridepublic void run() {// 浏览器发送一个get请求就可以把服务器的数据获取出来// 用代码模拟一个http的get请求try {// 1.得到服务器资源的路径URL url = new URL("http://192.168.1.130:8080/Day10/img/gaga.html");// 待会儿要解决一个问题,需要注意https/http以及虚拟机ip的问题 ;创建network_security_config.xml及在AndroidManifest.xml中添加数据才能进行http请求// 2.通过这个路径打开浏览器的链接HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 3.设置请求方式为GETconn.setRequestMethod("GET");// 注意请求方式只能大写,不能小写// 为了有一个更好的用户ui提醒,获取服务器的返回状态码int code = conn.getResponseCode();if (code == 200) {// 返回成功InputStream is = conn.getInputStream();File file = new File(getCacheDir(), "info.txt");FileOutputStream fos = new FileOutputStream(file);int len = 0;byte[] buffer = new byte[1024];while ((len = is.read(buffer)) != -1) {fos.write(buffer, 0, len);}is.close();fos.close();System.out.println("code == 200");// 获取了所有的链接之后,就要去加载图片beginLoadImage();} else if (code == 404) {// 资源未找到Message msg = Message.obtain();msg.what = LOAD_ERROR;msg.obj = "获取html失败,返回码:" + code;mHandler.sendMessage(msg);} else {// 其他响应码Message msg = Message.obtain();msg.what = LOAD_ERROR;msg.obj = "服务器或网络异常,返回码:" + code;mHandler.sendMessage(msg);}} catch (Exception e) {e.printStackTrace();}}}.start();}/*** 开始加载图片,在从服务器获取完毕资源路径之后执行*/private void beginLoadImage() {try {mPaths = new ArrayList<>();File file = new File(getCacheDir(), "info.txt");FileInputStream fis = new FileInputStream(file);BufferedReader br = new BufferedReader(new InputStreamReader(fis));String line;while ((line = br.readLine()) != null) {mPaths.add(line);}fis.close();loadImageByPath(mPaths.get(currentPosition));} catch (Exception e) {e.printStackTrace();}}/*** 通过路径加载图片** @param path*/private void loadImageByPath(final String path) {new Thread() {@Overridepublic void run() {File file = new File(getCacheDir(), path.replace("/", "") + ".jpg");if (file.exists() && file.length() > 0) {// 有缓存System.out.println("通过缓存把图片获取出来...");Message msg = Message.obtain();msg.what = LOAD_IMAGE;msg.obj = BitmapFactory.decodeFile(file.getAbsolutePath());mHandler.sendMessage(msg);} else {// 没有缓存的话,就需要去下载System.out.println("通过访问网络把图片资源获取出来");try {URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");int code = conn.getResponseCode();if (code == 200) {InputStream is = conn.getInputStream();// 内存中的图片Bitmap bitmap = BitmapFactory.decodeStream(is);FileOutputStream fos = new FileOutputStream(file);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.close();is.close();Message msg = Message.obtain();msg.what = LOAD_IMAGE;msg.obj = BitmapFactory.decodeFile(file.getAbsolutePath());mHandler.sendMessage(msg);} else {Message msg = Message.obtain();msg.what = LOAD_ERROR;msg.obj = "获取图片失败,返回码:" + code;mHandler.sendMessage(msg);}} catch (Exception e) {e.printStackTrace();Message msg = Message.obtain();msg.what = LOAD_ERROR;msg.obj = "获取图片失败";mHandler.sendMessage(msg);}}}}.start();}/*** 上一张图片** @param view*/public void pre(View view) {currentPosition--;if (currentPosition < 0) {currentPosition = mPaths.size() - 1;}loadImageByPath(mPaths.get(currentPosition));}/*** 下一张图片** @param view*/public void next(View view) {currentPosition++;if (currentPosition == mPaths.size()) {currentPosition = 0;}loadImageByPath(mPaths.get(currentPosition));}
}
<uses-permission android:name="android.permission.INTERNET"/>

注意:

app\src\main\res\xml路径下创建network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config><base-config cleartextTrafficPermitted="true" />
</network-security-config>

AndroidManifest.xml 添加数据才能进行http请求

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.glsite.netimageviewer"><uses-permission android:name="android.permission.INTERNET"/><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"android:networkSecurityConfig="@xml/network_security_config"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

web网络图片查看器Android相关推荐

  1. android网络图片查看器,Android网络应用(图片查看器)

    布局: android:id="@+id/widget32" android:layout_width="fill_parent" android:layout ...

  2. qt实现本地\网络图片查看器

    又是一个悠闲的下午...突然想到以前做项目时,写了个简单的网络图片查看器,翻出来看看.....功能太单一了,,鄙视自己.(于是花了一两个小时,将功能完善了一下,将他改装成了图片查看器) 什么是图片查看 ...

  3. 适用于android 4.0以上版本的子线程网络图片查看器

    android 4.0版本的新特性之一:加载网络内容时会自动判断是否在主线程中运行. 并且获取到内容时不能直接在子线程中设置主线程中的View,会报出以下异常 异常: CalledFromWrongT ...

  4. Android smartimageview网络图片查看器

    调用代码: SmartImageView siv = (SmartImageView) findViewById(R.id.siv);siv.setImageUrl(et_path.getText() ...

  5. android 网络图片查看器,Handler的用法

    通过网络访问图片,并通过Handler更新主线程的控件. public class MainActivity extends Activity {protected static final Stri ...

  6. Android学习笔记---23_网络通信之网络图片查看器

  7. android电池容量查看器,Android AccuBattery(电池损耗检测软件)V1.2.5 安卓专业版

    Android AccuBattery(电池损耗检测软件)是一款功能实用的提供安卓手机电池保持最佳状态而设计的电池管家软件.AccuBattery科学地维护电池健康,显示电池使用情况以及测量电池容量( ...

  8. Android仿微信朋友圈图片查看器

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/40264551 看博文之前,希望大家先打开自己的微信点到朋友圈中去,仔细观察是不是发 ...

  9. wifi boombox android,android filament入门,GLB和GLTF模型查看器

    filament入门挺难的,主要是因为受干扰的信息太多了,有arCore的干扰,也有scenceform的干扰.这里通过制作3D模型查看器的方式,理清他们之间的关系. 有用的信息来源主要有3个: 1, ...

最新文章

  1. Android之相对布局
  2. C/S、B/S的区别
  3. Canvas学习:封装Canvas绘制基本图形API
  4. 设置cookie存活时间_Cookie的存活时间
  5. 【电商】电商后台设计—电商支付
  6. OS | 【四 文件管理】强化阶段大题解构 —— FAT文件系统、UFS文件系统访问文件过程
  7. Spark Core (TopN、mysql写入、读取文件通过RDD结合数据库中的表)练习3套
  8. 规格说明书-吉林市2日游
  9. 图像搜索引擎 - 原理篇
  10. Pytorch实现GAN之生成手写数字图片
  11. 用函数调用的方式实现汽车移动的例子 (python)
  12. php erp开发文档,API文档
  13. [转载]穿透还原卡和还原软件
  14. Java毕设项目电力公司员工安全培训系统(java+VUE+Mybatis+Maven+Mysql)
  15. 仿天猫 购物车(Android studio 仿天猫 详情页面 添加购物车选择 颜色 尺寸demo)
  16. 动物 v.s. AI奥运会:你会赌一只鸟还是机器人夺冠?
  17. 设计模式 -- 工厂
  18. 3-12 董事会体制:DPOS共识机制(股份授权代表机制)
  19. 使用风格迁移模仿创作艺术风格图画
  20. 成信大807常用函数复习

热门文章

  1. 深入理解IIS工作原理
  2. 简书全站爬取 mysql异步保存
  3. aop日志(记录方法调用日志)
  4. R语言——一元线性回归
  5. html5+css3动画学习总结
  6. c语言中的scanf在java中应该怎么表达,Scanner类。
  7. Ubuntu-创建wifi热点-Android能连接(2)
  8. sprintf函数、snprintf函数、asprintf函数、vsprintf
  9. SVN篇:Shell脚本实现SVN启动,停止,重启
  10. ELK下Logstash性能调优