Android 获取手机图片
2012-04-27 14:39
232 查看
1.找到图片存放的路径
2.迭代此路径下是否存在后缀名与图片的后缀名相符合
3.相符合就把文件存放在list中
4.规格图片的大小
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;
}
}
}
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网络获取图片并保存在本地和获取手机SD卡中的图片显示到ImageView上及利用代码删除图片
- Android-获取手机上所有图片
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- Android手机调用系统相机拍照、裁剪以及获取Url上传图片
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- Android调用手机摄像头和相册获取图片
- Android-获取手机上的图片、视频、音频
- android中的ContentResolver获取手机本地图片
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用 ...
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- android 手机拍照后获取图片导致OOM问题
- android 获取手机内所有图片
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- Android总结之打开手机相册获取图片
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- Android 获取手机图片保存的一个通用工具类
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)