1.先弹出框有两个选项:
public void camera() {
final CharSequence[] items = { "相册", "拍照" };
AlertDialog dlg = new AlertDialog.Builder(SettingFragment.this.mAct)
.setTitle("选择图片")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法
if (item == 1) {
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
//去拍照
startActivityForResult(intent, REQUEST_CAMERA);
} else {
Intent getImage = new Intent(
Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/*");
startActivityForResult(getImage, 0);
}
}
}).create();
dlg.show();
}
2.拍照和相册事件完成后会回调OnActivityResult里面,requestCode是启动事件标识,resultCode是回调状态码 -1 0 1:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
Bitmap tempPhoto = null;
if (data != null) {
tempPhoto = (Bitmap) data.getExtras().get("data");
if (tempPhoto == null) {
return;
}
//img_head是ImageView设置图片
img_head.setImageBitmap(tempPhoto);
//读取图片压缩流数据
file = CropUtil.makeTempFile(
SettingFragment.this.getActivity(),
tempPhoto,
FileNameGenerator.generator(System.currentTimeMillis()
+ "hupu")
+ ".png");
file_path = file.getAbsolutePath();
}
}
if (requestCode == REQUEST_CAMERA||requestCode == 0) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(data.getData(), "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1); //设置按长和宽的比例裁剪
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 180); //设置输出的大小
intent.putExtra("outputY", 180);
intent.putExtra("scale", true); //设置是否允许拉伸
// 如果要在给定的uri中获取图片,则必须设置为false,如果设置为true,那么便不会在给定的uri中获取到裁剪的图片
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//设置输出格式
intent.putExtra("noFaceDetection", true); // 无需人脸识别 默认不需要设置
startActivityForResult(intent, 2);
}
}
3.读取图片缓存本地,并压缩文件流数据返回文件类型:
public static File makeTempFile(Context mcontext, Bitmap photo,
String nameKey) {
// 判断是否有存储卡
String status = Environment.getExternalStorageState();
// 等比例压缩图片,将较长的一边压缩到600px一下,最大容量不超过200K
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(CompressFormat.PNG, 100, bos);
byte[] tempData = bos.toByteArray();
File file2 = new File(FileNameGenerator.getExternalDir(mcontext)
+ "/runing");
if (file2.exists() && file2.isDirectory()) {
file2.delete();
}
file2.mkdir();
// 将压缩后的图片缓存到存储卡根目录下(权限)
File bFile = new File(FileNameGenerator.getExternalDir(mcontext)
+ "/runing", nameKey);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(bFile);
fos.write(tempData);
fos.flush();
if (bFile.exists() && bFile.length() > 0)
return bFile;
} catch (Exception e) {
e.printStackTrace();
} finally {
CropUtil.closeIO(null, fos);
}
return null;
}
4.获取系统文件存储路径如下:
/**
* 获取程序外部的缓存目录
*
* @param context
* @return
*/
public static File getExternalCacheDir(Context context) {
return new File(Environment.getExternalStorageDirectory().getPath()
);
}
public static File getExternalDir(Context context) {
return new File(getCahePath(context));
}
public static final String getCahePath(Context ctx) {
if (cache_path == null) {
cache_path = Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState()) ? getExternalCacheDir(ctx)
.getPath() : ctx.getCacheDir().getPath();
}
return cache_path;
}