打开相册和相机有多种方式,目前收集到两种方式;一种是来自环信程序员,一种是来自网络。不知道他们的区别在哪里,反正都可以实现相应的功能。
1 环信
/**
* 照相获取图片
*/
public void selectPicFromCamera() {
if (!CommonUtils.isExitsSdcard()) {
String st = getResources().getString(R.string.sd_card_does_not_exist);
Toast.makeText(getApplicationContext(), st, 0).show();
return;
}
cameraFile = new File(PathUtil.getInstance().getImagePath(), DemoApplication.getInstance().getUserName()
+ System.currentTimeMillis() + ".jpg");
cameraFile.getParentFile().mkdirs();
startActivityForResult(
new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile)),
REQUEST_CODE_CAMERA);
}
/**
* 选择文件
*/
private void selectFileFromLocal() {
Intent intent = null;
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
} else {
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
}
startActivityForResult(intent, REQUEST_CODE_SELECT_FILE);
}
/**
* 从图库获取图片
*/
public void selectPicFromLocal() {
Intent intent;
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
} else {
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
}
startActivityForResult(intent, REQUEST_CODE_LOCAL);
}
2 网络(我已使用多次)
/**
* 打开相册
* @param context
*/
public static void openAlbum(Activity context){
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
context.startActivityForResult(intent, 2);
}
/**
* 打开相机
* @param context
*/
public static void openCamera(Activity context){
if (!CommonUtils.isExitsSdcard()) {
ToastUtils.show(((Context)context), R.string.sdcard_not_exist);
return;
}
Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.png"));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
context.startActivityForResult(intent, 1);
}