这是一个类似 qq说说拍照上传 的 拍照效果,其调用系统拍照,拍照后可编辑是否删除
这是效果图
Demo 下载 地址 : 点击打开链接
整个 界面 是一个 GridView ,其最有一个item 固定 为 “+” ,点击 跳转 至 拍照。
其功能的 实现 都是 在 Adapter 中 实现。
而我的 Adapter 的 封装 可以 看我的 另一篇文章
Android ListView,GridView的Adapter封装
其中 在 bindDatas方法 中 设置 GridView 的 内容 和 功能
/**
* 绑定 数据
*
* @param viewHolder
* @param s
* @param position
*/
@Override
public void bindDatas(ViewHolder viewHolder, String s, final int position) {
ImageView image = (ImageView) viewHolder.getView(R.id.image);
final CheckBox checkBox = (CheckBox) viewHolder.getView(R.id.checkbox);
// 设置 显示 图片
setImageResource(image, s);
// 设置 勾选框 状态
setCheckBoxState(position, checkBox);
// 设置 监听器
setListener(viewHolder, position, checkBox);
}
/**
* 设置 图片 显示
*
* @param image ImageView
* @param item 图片路径
*/
void setImageResource(ImageView image, String item) {
if ("".equals(item)) {
image.setImageResource(R.mipmap.add);
} else {
BitmapFactory.Options options = new BitmapFactory.Options();
File file = new File(item);
/**
* BitmapFactory.Options的inJustDecodeBounds设置为true,可以在decoding的时候避免内存的分配
* ,它会返回一个null的bitmap,这个技术可以允许你在构造bitmap之前优先读图片的尺寸与类型。解析时可以根据解析的图片尺寸,
* 选择合适的缩放比例,将inJustDecodeBounds设为false,重新解析Bitmap,达到节省内存的目的
*/
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getPath(), options);
// 设置 图片缩放比例
options.inSampleSize = options.outWidth / 110;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), options);
// 压缩图片
// bitmap = compressImage(bitmap,500);
if (bitmap != null) {
// 压缩 图片
// 压缩 大小
int size = 80;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileOutputStream fos = null;
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, size, baos);
fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
image.setImageBitmap(bitmap);
}
}
}
GridView 的 点击 ,长按 事件 由 setListener 方法 实现
/**
* 设置 监听
*
* @param viewHolder
* @param position
* @param checkBox
*/
private void setListener(ViewHolder viewHolder, final int position, final CheckBox checkBox) {
View convertView = viewHolder.getViewGroup();
// 设置 长按 监听
convertView.setOnLongClickListener(this);
// 设置 单击 监听
convertView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isMulChoice && position < getList().size() - 1) {
if (checkBox.isChecked()) {
checkBox.setChecked(false);
isCheckeds.put(position, false);
// 移除 包含的Object 而不是 指定下标对应的 元素,
// 不然会 下标越界
selectedPositons.remove((Object) position);
} else {
isCheckeds.put(position, true);
checkBox.setChecked(true);
selectedPositons.add(position);
}
txtCount.setText("共选择了" + selectedPositons.size() + "项");
} else {
if (position == getList().size() - 1 && !isMulChoice) {
takePhotos();
}
}
}
});
}
/**
* 跳转到 拍照
* 然后 返回到 当前 Activity
* 处理 拍照结果
*/
private void takePhotos() {
String currentDate = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date(System.currentTimeMillis())).toString();
File photoFile = new File(Environment.getExternalStorageDirectory(), currentDate + ".jpg");
// 接口 回调 ,让 当前Activity存储 照片路径
((AddFile) getContext()).addFile(photoFile);
// 跳转 到 系统 拍照 并传入 保存文件的 名字
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
((Activity) getContext()).startActivityForResult(intent, 0);
}
Demo 下载 地址 : 点击打开链接