获取图片和视频数据源
public static class ImageTaskLoader implements LoaderManager.LoaderCallbacks<Cursor> { private Context mContext; private ImageLoaderCallbacks mImageLoaderCallbacks; public ImageTaskLoader(Context mContext, ImageLoaderCallbacks mImageLoaderCallbacks) { this.mContext = mContext; this.mImageLoaderCallbacks = mImageLoaderCallbacks; } private final String[] IMAGE_PROJECTION = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media.MIME_TYPE, MediaStore.Images.Media.SIZE, MediaStore.Images.Media._ID}; @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { CursorLoader cursorLoader = null; cursorLoader = new CursorLoader(mContext, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_PROJECTION, IMAGE_PROJECTION[4] + ">0 AND " + IMAGE_PROJECTION[3] + "=? OR " + IMAGE_PROJECTION[3] + "=? ", new String[]{"image/jpeg", "image/png"}, IMAGE_PROJECTION[2] + " DESC"); return cursorLoader; } private boolean fileExist(String path) { if (!TextUtils.isEmpty(path)) { return new File(path).exists(); } return false; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { List<Image> images = new ArrayList<>(); if (data != null) { if (data.getCount() > 0) { data.moveToFirst(); do { String path = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[0])); String name = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[1])); long dateTime = data.getLong(data.getColumnIndexOrThrow(IMAGE_PROJECTION[2])); if (!fileExist(path)) { continue; } Image image = null; if (!TextUtils.isEmpty(name)) { image = new Image(path, name, dateTime); images.add(image); } } while (data.moveToNext()); } } if (mImageLoaderCallbacks != null) { mImageLoaderCallbacks.onLoadFinished(images); } } @Override public void onLoaderReset(Loader<Cursor> loader) { } }
public static class VideoTaskLoader implements LoaderManager.LoaderCallbacks<Cursor> { private Context mContext; private VideoLoaderCallbacks mVideoLoaderCallbacks; public VideoTaskLoader(Context mContext, VideoLoaderCallbacks mVideoLoaderCallbacks) { this.mContext = mContext; this.mVideoLoaderCallbacks = mVideoLoaderCallbacks; } private final String[] VIDEO_PROJECTION = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATE_ADDED, MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.SIZE, MediaStore.Video.Media._ID, MediaStore.Video.VideoColumns.DURATION }; @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { CursorLoader cursorLoader = null; cursorLoader = new CursorLoader(mContext, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, VIDEO_PROJECTION, VIDEO_PROJECTION[4] + ">0 ", null, VIDEO_PROJECTION[2] + " DESC"); return cursorLoader; } private boolean fileExist(String path) { if (!TextUtils.isEmpty(path)) { return new File(path).exists(); } return false; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { List<Video> videos = new ArrayList<>(); if (data != null) { if (data.getCount() > 0) { data.moveToFirst(); do { String path = data.getString(data.getColumnIndexOrThrow(VIDEO_PROJECTION[0])); String name = data.getString(data.getColumnIndexOrThrow(VIDEO_PROJECTION[1])); long dateTime = data.getLong(data.getColumnIndexOrThrow(VIDEO_PROJECTION[2])); long size = data.getLong(data.getColumnIndexOrThrow(VIDEO_PROJECTION[4])); long duration_ms = data.getLong(data.getColumnIndexOrThrow(VIDEO_PROJECTION[6])); if (!fileExist(path)) { continue; } Video video = null; if (!TextUtils.isEmpty(name)) { video = new Video(path, name, dateTime, size, duration_ms); videos.add(video); } } while (data.moveToNext()); } } if (mVideoLoaderCallbacks != null) { mVideoLoaderCallbacks.onLoadFinished(videos); } } @Override public void onLoaderReset(Loader<Cursor> loader) { } }
ImageLoader显示缩率图
_displayImageOptions = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.no_hotelpic) .showImageOnLoading(R.drawable.no_hotelpic) .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//有效降低内存,减少oom .cacheInMemory(true).cacheOnDisk(true) .considerExifParams(true)//考虑旋转因素 .build();
图片的缩放方式
imageScaleType(ImageScaleType imageScaleType)
imageScaleType:
EXACTLY :图像将完全按比例缩小的目标大小
EXACTLY_STRETCHED:图片会缩放到目标大小完全
IN_SAMPLE_INT:图像将被二次采样的整数倍
IN_SAMPLE_POWER_OF_2:图片将降低2倍,直到下一减少步骤,使图像更小的目标大小
NONE:图片不会调整
选择器中的方形图片,宽撑满,自定义ImageView
public class SquaredImageView extends ImageView { public SquaredImageView(Context context) { super(context); } public SquaredImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); } }
获取图片角度
private int getImageSpinAngle(String path) { int degree = 0; ExifInterface exifInterface = null; try { exifInterface = new ExifInterface(path); } catch (IOException e) { // 图片不支持获取角度 return 0; } int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } return degree; }
图片旋转角度
private static Bitmap rotatingImage(int angle, Bitmap bitmap) { //rotate image Matrix matrix = new Matrix(); matrix.postRotate(angle); //create a new image return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); }