[关闭]
@Tyhj 2017-04-21T12:24:38.000000Z 字数 7577 阅读 1980

Android N下用摄像头和相册获取图片

Android


原文:https://www.zybuluo.com/Tyhj/note/730704
之前已经写过用摄像头和相册获取图片了,每一次都觉得是正解,每一次后来都发现自己很菜。最近发现项目在Android 7.0时候发生了一些权限问题。主要是Uri的获取,还有访问文件夹出了问题。

首先看使用相机来获取图片

  1. String path;//保存图片,
  2. public static final int TAKE_PHOTO = 1;
  3. public static final int CROP_PHOTO = 2;
  4. public static final int PICK_PHOTO = 0;
  5. String date;//根据时间来获取的图片的名字
  6. //随机获取文件名字
  7. public void getDate() {
  8. date = System.currentTimeMillis() + ".JPEG";
  9. }
  10. path = getExternalCacheDir() + "/Jubaoji";//写在onCreate中,用这个路径可以避过访问文件夹的权限

引用:
我先科普一个概念叫做应用关联缓存目录:就是指SD卡中专门用于存放当前应用缓存数据的位置,调用getExternalCacheDir()可以得到这个目录。具体路径是:/sdcard/Android/data//cache
那么为什么要使用应用关联缓存目录来存放图片呢?因为从android6.0开始读写SD卡被列为了危险权限,如果将图片放在SD卡的其他地方,都要进行运行时权限处理才行,而使用应用缓存数据的位置则可以跳过这一步

打开相机获取图象,并保存到file这个文件,path和date就不会改变了

  1. getDate();
  2. File file = new File(path, date);
  3. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//7.0及以上
  5. Uri uriForFile = FileProvider.getUriForFile(PersonalSetting.this, "demand.example.tyhj.jubao.fileProvider", file);
  6. intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
  7. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  8. intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  9. } else {
  10. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
  11. }
  12. startActivityForResult(intent, TAKE_PHOTO);

获取到相机的数据后,把file转成Uri调用系统工具剪裁图片

  1. //这是从相机返回的数据
  2. case TAKE_PHOTO:
  3. if (resultCode == PersonalSetting.RESULT_OK) {
  4. File file = new File(path, date);
  5. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  6. Uri inputUri = FileProvider.getUriForFile(PersonalSetting.this, "demand.example.tyhj.jubao.fileProvider", file);//通过FileProvider创建一个content类型的Uri
  7. cropPhoto(inputUri);
  8. } else {
  9. cropPhoto(Uri.fromFile(file));
  10. }
  11. }
  12. break;

剪裁图片,

  1. //剪裁图片
  2. public void cropPhoto(Uri imageUri) {
  3. Intent intent = new Intent("com.android.camera.action.CROP");
  4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  5. File file = new File(path, date);
  6. Uri outPutUri = FileProvider.getUriForFile(PersonalSetting.this, "demand.example.tyhj.jubao.fileProvider", file);
  7. intent.setDataAndType(imageUri, "image/*");
  8. intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);
  9. intent.putExtra("noFaceDetection", false);//去除默认的人脸识别,否则和剪裁匡重叠
  10. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  11. intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  12. } else {
  13. intent.setDataAndType(imageUri, "image/*");
  14. intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
  15. }
  16. // aspectX aspectY 是宽高的比例
  17. intent.putExtra("aspectX", 1);
  18. intent.putExtra("aspectY", 1);
  19. startActivityForResult(intent, CROP_PHOTO);
  20. }

//剪裁后返回的图片

  1. //剪裁图片返回数据,就是原来的文件
  2. case CROP_PHOTO:
  3. if (resultCode == PersonalSetting.RESULT_OK) {
  4. String fileName = path + "/" + date;
  5. File newFile = new File(path, date);
  6. //我在这里压缩了一下图片
  7. Defined.ImgCompress(fileName, newFile, 500, 500, 300);
  8. //获取到的就是new File或fileName
  9. }

从相册获取图片

打开相册选取图片

  1. getDate();
  2. dialog.cancel();
  3. Intent intent = new Intent(ACTION_GET_CONTENT);
  4. intent.addCategory(Intent.CATEGORY_OPENABLE);
  5. intent.setType("image/*");
  6. intent.putExtra("crop", true);
  7. intent.putExtra("scale", true);
  8. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果大于等于7.0使用FileProvider
  9. Uri uriForFile = FileProvider.getUriForFile(PersonalSetting.this, "demand.example.tyhj.jubao.fileProvider", new File(path,date));
  10. intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
  11. intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  12. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  13. } else {
  14. }
  15. startActivityForResult(intent, PICK_PHOTO);

//获取从相册返回的图片,为了避免破坏原来的图片,复制一份,再获取Uri,然后拿去剪裁

  1. //这是从相册返回的数据
  2. case PICK_PHOTO:
  3. if (resultCode == PersonalSetting.RESULT_OK) {
  4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果大于等于7.0使用FileProvider
  5. File path_pre = new File(GetImagePath.getPath(PersonalSetting.this, data.getData()));
  6. File newFile = new File(path, date);
  7. Toast.makeText(PersonalSetting.this, getString(R.string.msg_loading), Toast.LENGTH_SHORT).show();
  8. try {
  9. Defined.copyFile(path_pre, newFile);
  10. Uri dataUri = FileProvider.getUriForFile(PersonalSetting.this, "demand.example.tyhj.jubao.fileProvider", newFile);
  11. cropPhoto(dataUri);
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. } else {
  16. String path_pre = GetImagePath.getPath(PersonalSetting.this, data.getData());
  17. File newFile = new File(path, date);
  18. Toast.makeText(PersonalSetting.this, getString(R.string.msg_loading), Toast.LENGTH_SHORT).show();
  19. try {
  20. Defined.copyFile(new File(path_pre), newFile);
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. cropPhoto(Uri.fromFile(newFile));
  25. }
  26. }
  27. break;

之后和相机返回的是一样的,然后通过Uri获取文件路径方法也不是很简单,如下应该是比较好的,国外大神的方法

  1. package tools;
  2. import android.annotation.SuppressLint;
  3. import android.content.ContentUris;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import android.os.Build;
  8. import android.os.Environment;
  9. import android.provider.DocumentsContract;
  10. import android.provider.MediaStore;
  11. public class GetImagePath {
  12. // 4.4以上 content://com.android.providers.media.documents/document/image:3952
  13. // 4.4以下 content://media/external/images/media/3951
  14. @SuppressLint("NewApi")
  15. public static String getPath(final Context context, final Uri uri) {
  16. final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  17. // DocumentProvider
  18. if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  19. // ExternalStorageProvider
  20. if (isExternalStorageDocument(uri)) {
  21. final String docId = DocumentsContract.getDocumentId(uri);
  22. final String[] split = docId.split(":");
  23. final String type = split[0];
  24. if ("primary".equalsIgnoreCase(type)) {
  25. return Environment.getExternalStorageDirectory() + "/" + split[1];
  26. }
  27. }
  28. // DownloadsProvider
  29. else if (isDownloadsDocument(uri)) {
  30. final String id = DocumentsContract.getDocumentId(uri);
  31. final Uri contentUri = ContentUris.withAppendedId(
  32. Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  33. return getDataColumn(context, contentUri, null, null);
  34. }
  35. // MediaProvider
  36. else if (isMediaDocument(uri)) {
  37. final String docId = DocumentsContract.getDocumentId(uri);
  38. final String[] split = docId.split(":");
  39. final String type = split[0];
  40. Uri contentUri = null;
  41. if ("image".equals(type)) {
  42. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  43. } else if ("video".equals(type)) {
  44. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  45. } else if ("audio".equals(type)) {
  46. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  47. }
  48. final String selection = "_id=?";
  49. final String[] selectionArgs = new String[]{
  50. split[1]
  51. };
  52. return getDataColumn(context, contentUri, selection, selectionArgs);
  53. }
  54. }
  55. // MediaStore (and general)
  56. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  57. // Return the remote address
  58. if (isGooglePhotosUri(uri))
  59. return uri.getLastPathSegment();
  60. return getDataColumn(context, uri, null, null);
  61. }
  62. // File
  63. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  64. return uri.getPath();
  65. }
  66. return null;
  67. }
  68. //Android 4.4以下版本自动使用该方法
  69. public static String getDataColumn(Context context, Uri uri, String selection,
  70. String[] selectionArgs) {
  71. Cursor cursor = null;
  72. final String column = "_data";
  73. final String[] projection = {
  74. column
  75. };
  76. try {
  77. cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
  78. null);
  79. if (cursor != null && cursor.moveToFirst()) {
  80. final int index = cursor.getColumnIndexOrThrow(column);
  81. return cursor.getString(index);
  82. }
  83. } finally {
  84. if (cursor != null)
  85. cursor.close();
  86. }
  87. return null;
  88. }
  89. /**
  90. * @param uri The Uri to check.
  91. * @return Whether the Uri authority is ExternalStorageProvider.
  92. */
  93. public static boolean isExternalStorageDocument(Uri uri) {
  94. return "com.android.externalstorage.documents".equals(uri.getAuthority());
  95. }
  96. /**
  97. * @param uri The Uri to check.
  98. * @return Whether the Uri authority is DownloadsProvider.
  99. */
  100. public static boolean isDownloadsDocument(Uri uri) {
  101. return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  102. }
  103. /**
  104. * @param uri The Uri to check.
  105. * @return Whether the Uri authority is MediaProvider.
  106. */
  107. public static boolean isMediaDocument(Uri uri) {
  108. return "com.android.providers.media.documents".equals(uri.getAuthority());
  109. }
  110. /**
  111. * @param uri The Uri to check.
  112. * @return Whether the Uri authority is Google Photos.
  113. */
  114. public static boolean isGooglePhotosUri(Uri uri) {
  115. return "com.google.android.apps.photos.content".equals(uri.getAuthority());
  116. }
  117. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注