- 获取SD卡所有的jpeg,png格式的图片
public static ArrayList<String> getAllPhotoUrlsFromCR(Context context) {
ArrayList<String> allPhotoUrls = new ArrayList<String>()
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
ContentResolver mContentResolver = context.getContentResolver()
Cursor mCursor = mContentResolver.query(mImageUri, null,
MediaStore.Images.Media.MIME_TYPE + "=? or "
+ MediaStore.Images.Media.MIME_TYPE + "=?",
new String[] { "image/jpeg", "image/png" },
MediaStore.Images.Media.DATE_MODIFIED)
while (mCursor.moveToNext()) {
String path = mCursor.getString(mCursor
.getColumnIndex(MediaStore.Images.Media.DATA))
allPhotoUrls.add(path)
}
mCursor.close()
return allPhotoUrls
}
- 获取缩略图
-
1.
public static Bitmap getBitMap_thumbnail_size(String path) {
float expectWidth = 480f;
float expectHeigth = 800f;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
int height = options.outHeight;
int width = options.outWidth;
options.inSampleSize = (int) Math.max(height / expectHeigth, width / expectWidth);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
return bitmap;
}
-
2.
public static Bitmap getBitMap_thumbnail_quality(String path) {
int expectWidth = 480;
int expectHeigth = 800;
int qualitySize = 30;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = calculateInSampleSize(options,expectWidth,expectHeigth);
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(path, options);
try {
ByteArrayOutputStream baos = null ;
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, qualitySize, baos);
byte[] bytes = baos.toByteArray();
baos.close();
bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
} catch (IOException e) {
e.printStackTrace();
}
return bm;
}
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
}
return inSampleSize;
}