android加载网络图片并缓存,对图片进行点击打开相应链接
来源:互联网 发布:批发零售软件免费 编辑:程序博客网 时间:2024/05/05 18:47
1、原理很简单,就是从网上下载图片数据,将数据转成drawable并加载到指定的imageview
package com.downloadandopenimage; import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL; import android.content.Context;import android.content.Intent;import android.graphics.drawable.Drawable;import android.net.Uri;import android.os.AsyncTask;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.ProgressBar; public class LoadAndSaveImage { ImageView view; Context mContext; public LoadAndSaveImage() { // TODO Auto-generated constructor stub } public LoadAndSaveImage(Context context, String str, ImageView view) { // TODO Auto-generated constructor stub this.view = view; mContext = context; new Load_and_save_image().execute(str); } void release() { File file = new File(file:// + Environment.getExternalStorageDirectory().toString() + /download_image.jpg); if (file.exists()) { boolean deleted = file.delete(); } } class Load_and_save_image extends AsyncTask<string, void=""> { @Override protected Void doInBackground(String... sUrl) { // TODO Auto-generated method stub InputStream input = null; OutputStream output = null; HttpURLConnection connection = null; try { URL url = new URL(sUrl[0]); connection = (HttpURLConnection) url.openConnection(); connection.connect(); // download the file input = connection.getInputStream(); File checker = new File(file:// + Environment.getExternalStorageDirectory().toString() + /download_image.jpg); if (checker.exists()) { boolean deleted = checker.delete(); } output = new FileOutputStream(Environment .getExternalStorageDirectory().toString() + /download_image.jpg); byte data[] = new byte[4096]; int count; while ((count = input.read(data)) != -1) { output.write(data, 0, count); } } catch (Exception e) { } finally { try { if (input != null) input.close(); if (output != null) { output.close(); } } catch (IOException ignored) { } if (connection != null) connection.disconnect(); } return null; } protected void onPostExecute(Void result) { Drawable d = Drawable.createFromPath(Environment .getExternalStorageDirectory().toString() + /download_image.jpg); view.setImageDrawable(d); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.parse(file:// + Environment.getExternalStorageDirectory() .toString() + /download_image.jpg), image/*); mContext.startActivity(intent); } }); }; } }二、在Activity中获得上述类的对象,并传入三个数据,第一个是当前Context,第二个是URL,第三个是你要显示下载图片的ImageView对象。
0 0
- android加载网络图片并缓存,对图片进行点击打开相应链接
- android加载网络图片并保留缓存,随时点击打开
- iOS中加载HTML数据,并点击图片或链接进行相应的操作
- iOS中加载HTML数据,并点击图片或链接进行相应的操作
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
- Android 加载网络图片并做缓存
- Android 异步加载网络图片并缓存到本地
- Android 异步加载网络图片并缓存到本地
- Android 异步加载网络图片并缓存到本地
- Android 异步加载网络图片并缓存到本地
- android:显示网络图片,并对其缓存
- Android之ListView异步加载网络图片(优化缓存机制)和对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
- android异步加载图片显示,并且对图片进行缓存实例
- Oracle使用记录
- android:windowContentOverlay作用
- 分享一道伯克利 CS 61A 关于高阶函数的一道Python作业题(1)
- 几款主流PHP框架的优缺点评比
- Spring AOP的理解
- android加载网络图片并缓存,对图片进行点击打开相应链接
- PHP使用GD2库画图,图像无法输出解决方法
- 关于Spring中配置LocalSessionFactoryBean来生成SessionFactory
- Android Studio的SVN配置
- 大型互联网网站架构心得
- hessian学习
- 制作一个简单的登录界面
- (小知识点) textview.setBackground不起作用
- 两道桶排序题