Android 获取手机图片
来源:互联网 发布:大数据精准营销 编辑:程序博客网 时间:2024/04/23 16:44
1.找到图片存放的路径
2.迭代此路径下是否存在后缀名与图片的后缀名相符合
3.相符合就把文件存放在list中
4.规格图片的大小
那么下面开始看代码
String path = null;
ArrayList<String> list= null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*判断sd卡是否存在*/
boolean sdCardExist = Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED);
/*获取存放的路径*/
if(sdCardExist){
path = Environment.getExternalStorageDirectory()+" ";
}
LoadImageList();
}
public static boolean LoadImageList() {
try {
if (path == null || path.length() == 0)
return false;
list= new ArrayList<String>();
findFile(path);
return true;
} catch(Exception ex) {
ex.printStackTrace();
return false;
}
/*图片的迭代*/
public static ArrayList<String> findFile(final String strPath) {
fileList = new ArrayList<String>();
File file = new File(strPath);
File[] files = file.listFiles();
if (files == null) {
return null;
}
for(int i = 0; i < files.length; i++) {
final File f = files[i];
if(f.isFile()) {
try{
int idx = f.getPath().lastIndexOf(".");
if (idx <= 0) {
continue;
}
String suffix = f.getPath().substring(idx);
if (suffix.toLowerCase().equals(".jpg") ||
suffix.toLowerCase().equals(".jpeg") ||
suffix.toLowerCase().equals(".bmp") ||
suffix.toLowerCase().equals(".png") ||
suffix.toLowerCase().equals(".gif") )
{
fileList.add(f.getPath());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
/*规格图片的大小并转换为二进制数据*/
public static boolean loadImageFile() {
try {
imageData = null;
File imageFile = new File(path);
if (imageFile.exists() == false)
return false;
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(imageFile),null,o);
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<width || height_tmp/2<height)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
bitmap = BitmapFactory.decodeStream(new FileInputStream(imageFile), null, o2);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, buf);
buf.flush();
imageData = buf.toByteArray();
buf.close();
return true;
} catch(Exception ex) {
ex.printStackTrace();
return false;
}
}
}
- Android 获取手机图片
- android获取手机所有图片
- android 扫描手机上的图片 获取手机图片路径
- android中的ContentResolver获取手机本地图片
- Android获取手机图片路径的办法
- Android 获取手机中所有图片
- 简单获取Android手机中图片方法
- Android获取手机本地图片缩略图
- android 获取手机图片分享源码
- android中的ContentResolver获取手机本地图片
- android 获取手机内所有图片
- Android-获取手机上所有图片
- android 获取手机图片路径并删除图片
- Android手机读取相册图片,根据URI获取图片路径
- 获取手机中的图片
- 手机图片的获取
- 获取手机所有图片
- 获取Android手机上的图片和视频缩略图
- UIVIEWCONTROLLER各个方法的加载顺序
- HTTP 状态代码及其含义
- 手工实现字节对齐 及 代码质量思考
- poj 2524(并查集)
- 如何防止博客园文章被窃取
- Android 获取手机图片
- 将datatable转换成list
- [转] LockWindowUpdate系列
- mysql
- HTTP协议总括
- open_basedir restriction in effect的错误及其解决办法
- linux平台下oracle从10.2.0.1升级到10.2.0.4
- jquery tab 选项卡功能
- 浅谈Java反射机制(一)