!第一次写博客,有什么不好的地方请见谅。
实现功能:
1. 截屏点击更多可以显示截屏的图片。
2. 拍照点击更多可以显示拍照图片。
实现遇到的问题:
1. 怎么获取到每次拍照图片的路径?
2. 三星手机拍照显示照片会旋转90°。(直接看项demo)
3. 截屏怎么监听广播?(其实截屏现在是没有广播的action的)
那么我们就从第一个问题开始来解释吧。
想到图片路径我们可能会想到内容提供者,为什么那因为我们每次存储图片就相当于对系统的数据库增加或者删除了,系统会记录下来它们的位置(就是路径)那么有人可能会想知道怎么获取该路径啦。
/**
* 完整的图片路径和时间
*
* @param context
* @return
*/
public static List<String> getFullImage(Context context) {
List<String> result = new ArrayList<String>();
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String orderBy = MediaStore.Images.Media.DATE_TAKEN + " DESC";
ContentResolver contentResolver = context.getContentResolver();
/**
* desc 到序。
*/
Cursor cursor = contentResolver.query(uri, null, null, null, orderBy);
if (cursor == null || cursor.getCount() <= 0) return null; // 没有图片
while (cursor.moveToNext()) {
int index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
int dataindex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN);
String path = cursor.getString(index); // 文件地址
long data = new Long(cursor.getString(dataindex));
long systime = System.currentTimeMillis();
flag = compareTime(data, systime);
File file = new File(path);
if (file.exists()) {
result.add(path);
}
return result;
}
return result;
}
如果各位同学学过sql 就知道 desc 是降序排序。
这个方法可以优化的,最需要获取第一张就可以,因为我(交给你们了)
以上就可以取得图片的路径了。
因为是用popwindows 显示图片的,所以就需要对图片做一些优化。
(就不一一解释了)
/**
* 根据View(主要是ImageView)的宽和高来获取图片的缩略图
*
* @param path
* @param viewWidth
* @param viewHeight
* @return
*/
private Bitmap decodeThumbBitmapForFile(String path, int viewWidth, int viewHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
//设置为true,表示解析Bitmap对象,该对象不占内存
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
//设置缩放比例
options.inSampleSize = calculateInSampleSize(options, viewWidth, viewHeight);
//设置为false,解析Bitmap对象加入到内存中
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
/**
* 根据View(主要是ImageView)的宽和高来计算Bitmap缩放比例。默认不缩放
*
* @param options
*/
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
说起这个截屏都是一把心酸泪
方法1:想通过广播来获取截屏的action 然后在通过内容提供者查询到最后的image path。(行不通,官方目前还没有提供)
方法2:
通过检测Screenshots的文件夹,看时候有没有新的文件写入。
这里接触到一个新的方法( FileObserver)
observer = new FileObserver(pathToWatch) { // set up a file observer to watch this directory on sd card
@Override
public void onEvent(int event, String file) {
/**
* FileObserver.MODIFY
* 数据被写入到文件中
*/
if (event==FileObserver.MODIFY) {
Log.d("MainActivity", "File created [" + pathToWatch + file + "]");
flag = false;
oldtime = System.currentTimeMillis();
}
}
};
observer.startWatching(); //START OBSERVING
详细的代码在demo中查看。
引用:
screenshot的路径获取
截屏文件夹变化更新
demo源码
附上一张效果图片