您的位置:首页 > 其它

在指定路径创建文件夹,读取图片文件,缩放图片,判断文件夹是否存在,

2016-08-04 15:37 645 查看
继续做笔记。。。

File mBaseFile = new File("mnt/sdcard/XYPicture/");

mBaseFile.exists()  // 判断文件夹是否存在

String[] list = mBaseFile.list();

list.length == 0  //判断文件夹是否为空

/**
* 创建目录文件
*/
public static void createPath(String path) {
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
}

/**
* 获取图片地址列表

* @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 {
// 使用TreeMap,排序问题就不需要纠结了
Map<String, Bitmap> maps = new TreeMap<String, Bitmap>();
if (mBaseFile != null && mBaseFile.exists()) {
mPathLists = imagePath(mBaseFile);
if (!mPathLists.isEmpty()) {
for (int i = 0; i < mPathLists.size(); i++) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 设置了此属性一定要记得将值设置为false
Bitmap bitmap = BitmapFactory.decodeFile(mPathLists.get(i),
options);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(mPathLists.get(i),
options);
maps.put(mPathLists.get(i),
Utilities.zoomBitmap(bitmap, 278, 136));//强行缩放图片的方法
}
}
}
return maps;
}

/**
* 缩放图片

* @param bitmap
*            原始图片
* @param w
*            缩放之后的宽
* @param h
*            缩放之后的高
* @return
*/
static public Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float) w / width);
float scaleHeight = ((float) h / height);

Log.i(TAG, "width:" + width + " height:" + height + " w:" + w + " h:"
+ w);
/*
* 通过Matrix类的postScale方法进行缩放
*/
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
matrix, true);
return newbmp;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息