项目中用的知乎的matisse,但是后来有个需求点击直接跳转照相机:
在AndroidManifest.xml 中:
android:authorities="com.gswlznkj.crm.provider"
android:exported="false"
android:grantUriPermissions="true">
android:resource="@xml/file_paths" />
在res文件夹中建立xml文件夹
在xml文件夹中新建file_paths.xml文件
path="Android/data/com.example.package.name/files/Pictures/OriPicture/" />
path="Android/data/com.example.package.name/files/Pictures/OriPicture/" />
path="files/Pictures/OriPicture" />
path="" />
path="" />
activity中的代码
private static final int REQUEST_CODE_CHOOSE = 1001;
private File file;
if(AppUtils.isNeedCreamePermission(SignActivity.this))
return;
openSysCamera();
/**
* 打开系统相机
*/
private void openSysCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
file = createOriImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (file != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
imageUri = Uri.fromFile(file);
} else {
imageUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", file);
}
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, REQUEST_CODE_CHOOSE);
}
}
/**
* 创建原图像保存的文件
*
* @return
* @throws IOException
*/
private File createOriImageFile() throws IOException {
String imgNameOri =new SimpleDateFormat(
"yyyyMMdd_HHmmss").format(new Date());
File pictureDirOri = new File(getExternalFilesDir(
Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/OriPicture");
if (!pictureDirOri.exists()) {
pictureDirOri.mkdirs();
}
File image = File.createTempFile(
imgNameOri, /* prefix */
".jpg", /* suffix */
pictureDirOri /* directory */
);
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
AppUtils.getPathFromUri(this,imageUri);
}
}
这是AppUtils里的方法
public static boolean isNeedCreamePermission(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] permissionList = new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};
PermissionManager permissionManager = PermissionManager.with(activity);
permissionManager.setNecessaryPermissions(permissionList);
if (permissionManager.isLackPermission()) {
permissionManager.requestPermissions();
return true;
}
}
return false;
}
@SuppressLint("NewApi")
public static String getPathFromUri(Context context,final Uri uri) {
if (uri == null) {
return null;
}
// 判斷是否為Android 4.4之後的版本
final boolean after44 = Build.VERSION.SDK_INT >= 19;
if (after44 && DocumentsContract.isDocumentUri(context, uri)) {
// 如果是Android 4.4之後的版本,而且屬於文件URI
final String authority = uri.getAuthority();
// 判斷Authority是否為本地端檔案所使用的
if ("com.android.externalstorage.documents".equals(authority)) {
// 外部儲存空間
final String docId = DocumentsContract.getDocumentId(uri);
final String[] divide = docId.split(":");
final String type = divide[0];
if ("primary".equals(type)) {
String path = Environment.getExternalStorageDirectory().getAbsolutePath().concat("/").concat(divide[1]);
return path;
} else {
String path = "/storage/".concat(type).concat("/").concat(divide[1]);
return path;
}
} else if ("com.android.providers.downloads.documents".equals(authority)) {
// 下載目錄
final String docId = DocumentsContract.getDocumentId(uri);
if (docId.startsWith("raw:")) {
final String path = docId.replaceFirst("raw:", "");
return path;
}
final Uri downloadUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.parseLong(docId));
String path = queryAbsolutePath(context, downloadUri);
return path;
} else if ("com.android.providers.media.documents".equals(authority)) {
// 圖片、影音檔案
final String docId = DocumentsContract.getDocumentId(uri);
final String[] divide = docId.split(":");
final String type = divide[0];
Uri mediaUri = null;
if ("image".equals(type)) {
mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
mediaUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
mediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
} else {
return null;
}
mediaUri = ContentUris.withAppendedId(mediaUri, Long.parseLong(divide[1]));
String path = queryAbsolutePath(context, mediaUri);
return path;
}
} else {
// 如果是一般的URI
final String scheme = uri.getScheme();
String path = null;
if ("content".equals(scheme)) {
// 內容URI
path = queryAbsolutePath(context, uri);
} else if ("file".equals(scheme)) {
// 檔案URI
path = uri.getPath();
}
return path;
}
return null;
}