Android开发之--读取文件夹下图片生成略缩图并点击显示大图
jopen
13年前
这是一个简单的Demo,目的是:读取文件夹下图片生成略缩图并点击显示大图。 <p></p> 先新建一个工程,创建一个ThumbnailsWindows的类,继承LinearLayout。代码如下: <pre class="brush:java; toolbar: true; auto-links: false;"> package org.winplus.thum.view; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import cn.embel.thum.R; public class ThumbnailsWindows extends LinearLayout { private static final String TAG = "ThumbnailsWindows"; private Context mContext; private static ArrayList<String> paths = new ArrayList<String>(); private ImageView imageView; public ThumbnailsWindows(Context context) { super(context); mContext = context; setupViews(); } public ThumbnailsWindows(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; setupViews(); } public void setupViews() { /** * 显示大图时需要使用,当然可以直接在此类中定义!这样还好控制一些~到时候再改吧,赶这过年呢 */ final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext()); View v = mLayoutInflater.inflate(R.layout.original_photo, null); imageView = (ImageView) v.findViewById(R.id.original); Map<String,Bitmap> maps = new TreeMap<String, Bitmap>(); try { maps = buildThum(); } catch (FileNotFoundException e) { e.printStackTrace(); } Iterator<String> it = maps.keySet().iterator(); int i = 0; while (it.hasNext()) { String path = (String) it.next(); Bitmap bm = maps.get(path); ImageButton image = new ImageButton(mContext); image.setImageBitmap(bm); image.setId(i++); addView(image); image.setOnTouchListener(listener); } addView(v); } /** * 定义按钮控件的Touch事件 */ OnTouchListener listener = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { /** * 控件按下的时候显示当前略缩图的大图 */ if(event.getAction() == MotionEvent.ACTION_DOWN){ String path = paths.get(v.getId()); InputStream inputStream = null; try { inputStream = new FileInputStream(path); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap bitmap = BitmapFactory.decodeStream(inputStream); imageView.setImageBitmap(bitmap); } return false; } }; /** * 获取图片地址列表 * @param file * @return */ private static ArrayList<String> imagePath(File file) { ArrayList<String> list = new ArrayList<String>(); File[] files = file.listFiles(); for (File f : files) { list.add(f.getAbsolutePath()); } Collections.sort(list); return list; } /** * 读取sdcard文件夹中的图片,并生成略缩图 * @return * @throws FileNotFoundException */ private Map<String,Bitmap> buildThum() throws FileNotFoundException { File baseFile = new File("/mnt/sdcard/tflash/image/"); // 使用TreeMap,排序问题就不需要纠结了 Map<String,Bitmap> maps = new TreeMap<String, Bitmap>(); if (baseFile != null && baseFile.exists()) { paths = imagePath(baseFile); if (!paths.isEmpty()) { for (int i = 0; i < paths.size(); i++) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 设置了此属性一定要记得将值设置为false Bitmap bitmap =BitmapFactory.decodeFile(paths.get(i),options); options.inJustDecodeBounds = false; int be = options.outHeight/40; if (be <= 0) { be = 10; } options.inSampleSize = be; bitmap = BitmapFactory.decodeFile(paths.get(i),options); maps.put(paths.get(i), bitmap); } } } return maps; } } </pre> <span style="font-size:16px;"><span style="font-size:16px;">修改mail.xml文件<pre class="brush:xml; toolbar: true; auto-links: false;"><span style="font-size: 16px; "><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <org.winplus.thum.view.ThumbnailsWindows android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout></span></pre><img title="0_13267077305hgt.gif" border="0" alt="0_13267077305hgt.gif" src="https://simg.open-open.com/show/0aace7e9596f2f7e548188941879dcf6.gif" width="700" height="525" /></span></span> <p></p> <p><span style="font-size:16px;">本Demo还有Bug,稍后在修改吧,看能否经过修改,改成像Ihone图片浏览器一样的效果. <br /> </span></p> <p><span style="font-size:16px;"><a href="/misc/goto?guid=4959500912853414016">源码下载==》</a></span></p> <p><span style="font-size:16px;">原创文章,转载请注明出处:<a href="/misc/goto?guid=4959500912931661596">http://blog.csdn.net/tangcheng_ok</a></span></p>