在指定路径创建文件夹,读取图片文件,缩放图片,判断文件夹是否存在,
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;
}
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;
}
相关文章推荐
- asp.net 判断指定路径指定文件夹下是否有图片文件存在
- 读取指定文件夹里面的所有图片取出里面其中一张图片和判断文件夹或文件是否存在
- 【C#】C#判断文件路径是否存在,不存在则创建文件夹
- 用DirectoryExists和FileExists判断指定路径下是否存在指定的文件夹或文件
- MFC判断指定路径下目标文件、目标文件夹是否存在
- Java 判断文件或者文件夹是否存在并创建新文件或者文件夹
- Java 判断文件夹、文件是否存在、否则创建文件夹
- winform中判断文件夹以及文件是否存在,文件夹不存在进行创建,文件存在进行删除
- C# 判断文件和文件夹是否存在并创建
- c#创建xml读取xml方法并判断文件目录以及文件是否存在
- C#获取FTP目录下文件夹、文件的方法,进而判断FTP下指定文件夹是否存在
- C#判断文件及文件夹是否存在并创建(C#判断文件夹存在)
- 判断文件是否存在及创建文件夹
- python判断文件和文件夹是否存在、创建文件夹
- 判断指定路径下文件是否存在
- java判断指定路径文件是否存在
- python判断文件和文件夹是否存在、创建文件夹
- C#判断文件及文件夹是否存在并创建(C#判断文件夹存在)
- java 创建文件夹 创建文件 判断文件夹是否存在。。。。。
- 在JSP中判断某文件是否存在,并创建文件夹和文件