网络图片查看器HttpURLConnection+Bitmap
网络图片查看器软件界面网络图片查看器软件界面MainActivityjavaStreamToolsjavaactivity_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>

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)