注意:android7.0以上拍照会闪退,需要调用以下代码
// android 7.0以上系统解决拍照的问题
private void initPhotoError(){
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
}
一、权限配置
<uses-permission android:name="android.permission.INTERNET" /><!--网络权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!--写入权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><!--读取权限-->
代码中区分跳转图库和摄像头属性
private final int GALLERY = 0;// 图库
private final int TAKE_PHOTO = 1;// 拍照
二、调用本机图库方法
// 创建Intent,用于打开手机本地图库选择图片
Intent base_pic = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//选择图片格式
base_pic.setType("image/*");
// 启动intent打开本地图库
startActivityForResult(base_pic,GALLERY);
获取到选中的图片并保存到自定义文件夹
/**
* 跳转页面返回方法
* */
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
switch (requestCode){
//处理图库返回
case GALLERY:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
try {
InputStream imageIS = getActivity().getContentResolver().openInputStream(selectedImage);
// 获取图片流
Bitmap bitmap = BitmapFactory.decodeStream(imageIS);
// 调用保存图片方法
saveBitmapFile(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
/**设置拍照保存后再次跳到拍照界面**/
if (resultCode == RESULT_OK && requestCode == 1){
// 图片名称
String imgName = new Date().getTime() + ".jpg";
Uri uri = getTakePhotoUri(paramFile,imgName);
// 创建Intent,用于调用摄像头
Intent tp_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//存放相机返回的图片
tp_intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
// 启动intent调用本机摄像头
startActivityForResult(tp_intent,TAKE_PHOTO);
}
}
/**
* 保存图库选取的图片
* */
public void saveBitmapFile(Bitmap bitmap){
File galleyFile = new File(Environment.getExternalStorageDirectory(),"图库图片");
File temp = new File(galleyFile.getPath());//要保存文件先创建文件夹
if (!temp.exists()) {
temp.mkdir();
}
// 图片名称
String imgName = new Date().getTime() + ".jpg";
//将要保存图片的路径和图片名称
File file=new File(galleyFile.getPath()+File.separator+imgName);
try {
BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
Toast.makeText(getContext(),
"路径:"+galleyFile.getPath(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
三、调用本机摄像头拍照方法
// 创建Intent,用于调用摄像头
Intent tp_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 图片名称
String imgName = new Date().getTime() + ".jpg";
//存放相机返回的图片
tp_intent.putExtra(MediaStore.EXTRA_OUTPUT, getTakePhotoUri(imgName));
// 启动intent调用本机摄像头
startActivityForResult(tp_intent,TAKE_PHOTO);
获取Uri方法(拍照后的图片保存路径)
/**
* 设置相机拍照返回图片路径
* */
private Uri getTakePhotoUri(String imgName){
File photoFile = new File(Environment.getExternalStorageDirectory(), "岩芯图片");
if (!photoFile.exists()) {
photoFile.mkdirs();
}
//将要保存图片的路径和图片名称
File file = new File(photoFile+File.separator+imgName);
Uri uri = Uri.fromFile(file);
return uri;
}
四、配置文件修改(高版本无法获取图片)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.chy.democamera"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
sourceSets {
main {
jniLibs.srcDir 'libs'
}
}
}
repositories {
mavenCentral()
google()
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}