Android调用系统相机、相册、剪裁图片并上传

由于在Android 7.0 采用了StrictMode API政策禁,其中有一条限制就是对目录访问的限制。

这项变更意味着我们无法通过File API访问手机存储上的数据,也就是说,给其他应用传递 file:// URI 类型的Uri,可能会导致接受者无法访问该路径,并且会会触发 FileUriExposedException异常。

StrictMode API政策禁中的应用间共享文件就是对上述限制的应对方法,它指明了我们在在应用间共享文件可以发送 content:// URI类型的Uri,并授予 URI 临时访问权限,即使用FileProvider

接下来,我们使用FileProvider实现调用系统相机、相册、剪裁图片的功能兼容Android 7.0

第一步:FileProvider相关准备工作

  • 在AndroidManifest.xml中增加provider节点,如下:
 
 
  1. <provider
  2. android:name="android.support.v4.content.FileProvider"
  3. android:authorities="com.hansion.chosehead"
  4. android:grantUriPermissions="true"
  5. android:exported="false">
  6. <meta-data
  7. android:name="android.support.FILE_PROVIDER_PATHS"
  8. android:resource="@xml/filepaths" />
  9. </provider>

其中: android:authorities 表示授权列表,填写你的应用包名,当有多个授权时,用分号隔开 android:exported 表示该内容提供器(ContentProvider)是否能被第三方程序组件使用,必须为false,否则会报异常:ava.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.SecurityException: Provider must not be exportedandroid:grantUriPermissions="true" 表示授予 URI 临时访问权限 android:resource 属性指向我们自及创建的xml文件的路径,文件名随便起

  • 接下来,我们需要在资源(res)目录下创建一个xml目录,并建立一个以上面名字为文件名的xml文件,内容如下:
 
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <paths>
  3. <external-path path="" name="image" />
  4. </paths>

其中: external-path 代表根目录为: Environment.getExternalStorageDirectory() ,也可以写其他的,如: files-path 代表根目录为:Context.getFilesDir() cache-path 代表根目录为:getCacheDir() 其path属性的值代表路径后层级名称,为空则代表就是根目录,假如为“pictures”,就代表对应根目录下的pictures目录

第二步:使用FileProvider

  • 在这之前,我们需要在AndroidManifest.xml中增加必要的读写权限:
 
 
  1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

1. 通过相机获取图片

在通过Intent跳转系统相机前,我们需要对版本进行判断,如果在Android7.0以上,使用FileProvider获取Uri,代码如下:

 
 
  1. /**
  2. * 从相机获取图片
  3. */
  4. private void getPicFromCamera() {
  5. //用于保存调用相机拍照后所生成的文件
  6. tempFile = new File(Environment.getExternalStorageDirectory().getPath(), System.currentTimeMillis() + ".jpg");
  7. //跳转到调用系统相机
  8. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  9. //判断版本
  10. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //如果在Android7.0以上,使用FileProvider获取Uri
  11. intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  12. Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "com.hansion.chosehead", tempFile);
  13. intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
  14. } else { //否则使用Uri.fromFile(file)方法获取Uri
  15. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
  16. }
  17. startActivityForResult(intent, CAMERA_REQUEST_CODE);
  18. }

如果你好奇通过FileProvider获取的Uri是什么样的,可以打印出来看一看,例如:

 
 
  1. content://com.hansion.chosehead/image/1509356493642.jpg

其中: com.hansion.chosehead 是我的包名 image 是上文中xml文件中的name属性的值 1509356493642.jpg 是我创建的图片的名字 也就是说,content://com.hansion.chosehead/image/ 代表的就是根目录

2.通过相册获取图片

 
 
  1. /**
  2. * 从相册获取图片
  3. */
  4. private void getPicFromAlbm() {
  5. Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
  6. photoPickerIntent.setType("image/*");
  7. startActivityForResult(photoPickerIntent, ALBUM_REQUEST_CODE);
  8. }

3.剪裁图片

 
 
  1. /**
  2. * 裁剪图片
  3. */
  4. private void cropPhoto(Uri uri) {
  5. Intent intent = new Intent("com.android.camera.action.CROP");
  6. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  7. intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  8. intent.setDataAndType(uri, "image/*");
  9. intent.putExtra("crop", "true");
  10. intent.putExtra("aspectX", 1);
  11. intent.putExtra("aspectY", 1);
  12. intent.putExtra("outputX", 300);
  13. intent.putExtra("outputY", 300);
  14. intent.putExtra("return-data", true);
  15. startActivityForResult(intent, CROP_REQUEST_CODE);
  16. }

上文中,你会发现,我们只对Uri进行了特殊处理。没错,这就是核心变化

第三步:接收图片信息

  • 我们在onActivityResult方法中获得返回的图片信息,在这里我们会先调用剪裁去剪裁图片,然后对剪裁返回的图片进行设置、保存、上传等操作
 
 
  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  3. switch (requestCode) {
  4. case CAMERA_REQUEST_CODE: //调用相机后返回
  5. if (resultCode == RESULT_OK) {
  6. //用相机返回的照片去调用剪裁也需要对Uri进行处理
  7. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  8. Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "com.hansion.chosehead", tempFile);
  9. cropPhoto(contentUri);
  10. } else {
  11. cropPhoto(Uri.fromFile(tempFile));
  12. }
  13. }
  14. break;
  15. case ALBUM_REQUEST_CODE: //调用相册后返回
  16. if (resultCode == RESULT_OK) {
  17. Uri uri = intent.getData();
  18. cropPhoto(uri);
  19. }
  20. break;
  21. case CROP_REQUEST_CODE: //调用剪裁后返回
  22. Bundle bundle = intent.getExtras();
  23. if (bundle != null) {
  24. //在这里获得了剪裁后的Bitmap对象,可以用于上传
  25. Bitmap image = bundle.getParcelable("data");
  26. //设置到ImageView上
  27. mHeader_iv.setImageBitmap(image);
  28. //也可以进行一些保存、压缩等操作后上传
  29. // String path = saveImage("crop", image);
  30. }
  31. break;
  32. }
  33. }
  • 保存Bitmap到本地的方法:
 
 
  1. public String saveImage(String name, Bitmap bmp) {
  2. File appDir = new File(Environment.getExternalStorageDirectory().getPath());
  3. if (!appDir.exists()) {
  4. appDir.mkdir();
  5. }
  6. String fileName = name + ".jpg";
  7. File file = new File(appDir, fileName);
  8. try {
  9. FileOutputStream fos = new FileOutputStream(file);
  10. bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
  11. fos.flush();
  12. fos.close();
  13. return file.getAbsolutePath();
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. return null;
  18. }

至此,对Android7.0的兼容就结束了 总结一下,在调用相机和剪裁时,传入的Uri需要使用FileProvider来获取


完整代码:

  • MainActivity.java
 
 
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. private ImageView mHeader_iv;
  3. //相册请求码
  4. private static final int ALBUM_REQUEST_CODE = 1;
  5. //相机请求码
  6. private static final int CAMERA_REQUEST_CODE = 2;
  7. //剪裁请求码
  8. private static final int CROP_REQUEST_CODE = 3;
  9. //调用照相机返回图片文件
  10. private File tempFile;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. initView();
  16. }
  17. private void initView() {
  18. mHeader_iv = (ImageView) findViewById(R.id.mHeader_iv);
  19. Button mGoCamera_btn = (Button) findViewById(R.id.mGoCamera_btn);
  20. Button mGoAlbm_btn = (Button) findViewById(R.id.mGoAlbm_btn);
  21. mGoCamera_btn.setOnClickListener(this);
  22. mGoAlbm_btn.setOnClickListener(this);
  23. }
  24. @Override
  25. public void onClick(View view) {
  26. switch (view.getId()) {
  27. case R.id.mGoCamera_btn:
  28. getPicFromCamera();
  29. break;
  30. case R.id.mGoAlbm_btn:
  31. getPicFromAlbm();
  32. break;
  33. default:
  34. break;
  35. }
  36. }
  37. /**
  38. * 从相机获取图片
  39. */
  40. private void getPicFromCamera() {
  41. //用于保存调用相机拍照后所生成的文件
  42. tempFile = new File(Environment.getExternalStorageDirectory().getPath(), System.currentTimeMillis() + ".jpg");
  43. //跳转到调用系统相机
  44. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  45. //判断版本
  46. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //如果在Android7.0以上,使用FileProvider获取Uri
  47. intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  48. Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "com.hansion.chosehead", tempFile);
  49. intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
  50. Log.e("dasd", contentUri.toString());
  51. } else { //否则使用Uri.fromFile(file)方法获取Uri
  52. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
  53. }
  54. startActivityForResult(intent, CAMERA_REQUEST_CODE);
  55. }
  56. /**
  57. * 从相册获取图片
  58. */
  59. private void getPicFromAlbm() {
  60. Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
  61. photoPickerIntent.setType("image/*");
  62. startActivityForResult(photoPickerIntent, ALBUM_REQUEST_CODE);
  63. }
  64. /**
  65. * 裁剪图片
  66. */
  67. private void cropPhoto(Uri uri) {
  68. Intent intent = new Intent("com.android.camera.action.CROP");
  69. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  70. intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  71. intent.setDataAndType(uri, "image/*");
  72. intent.putExtra("crop", "true");
  73. intent.putExtra("aspectX", 1);
  74. intent.putExtra("aspectY", 1);
  75. intent.putExtra("outputX", 300);
  76. intent.putExtra("outputY", 300);
  77. intent.putExtra("return-data", true);
  78. startActivityForResult(intent, CROP_REQUEST_CODE);
  79. }
  80. @Override
  81. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  82. switch (requestCode) {
  83. case CAMERA_REQUEST_CODE: //调用相机后返回
  84. if (resultCode == RESULT_OK) {
  85. //用相机返回的照片去调用剪裁也需要对Uri进行处理
  86. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  87. Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "com.hansion.chosehead", tempFile);
  88. cropPhoto(contentUri);
  89. } else {
  90. cropPhoto(Uri.fromFile(tempFile));
  91. }
  92. }
  93. break;
  94. case ALBUM_REQUEST_CODE: //调用相册后返回
  95. if (resultCode == RESULT_OK) {
  96. Uri uri = intent.getData();
  97. cropPhoto(uri);
  98. }
  99. break;
  100. case CROP_REQUEST_CODE: //调用剪裁后返回
  101. Bundle bundle = intent.getExtras();
  102. if (bundle != null) {
  103. //在这里获得了剪裁后的Bitmap对象,可以用于上传
  104. Bitmap image = bundle.getParcelable("data");
  105. //设置到ImageView上
  106. mHeader_iv.setImageBitmap(image);
  107. //也可以进行一些保存、压缩等操作后上传
  108. // String path = saveImage("crop", image);
  109. }
  110. break;
  111. }
  112. }
  113. public String saveImage(String name, Bitmap bmp) {
  114. File appDir = new File(Environment.getExternalStorageDirectory().getPath());
  115. if (!appDir.exists()) {
  116. appDir.mkdir();
  117. }
  118. String fileName = name + ".jpg";
  119. File file = new File(appDir, fileName);
  120. try {
  121. FileOutputStream fos = new FileOutputStream(file);
  122. bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
  123. fos.flush();
  124. fos.close();
  125. return file.getAbsolutePath();
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129. return null;
  130. }
  131. }
  • activity_main.xml
 
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:padding="5dp"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_horizontal"
  11. android:layout_marginTop="30dp"
  12. android:text="选择头像"
  13. android:textSize="18sp" />
  14. <ImageView
  15. android:id="@+id/mHeader_iv"
  16. android:layout_width="100dp"
  17. android:layout_height="100dp"
  18. android:layout_gravity="center_horizontal"
  19. android:layout_marginTop="50dp"
  20. android:src="@mipmap/ic_launcher" />
  21. <android.support.v4.widget.Space
  22. android:layout_width="match_parent"
  23. android:layout_height="0dp"
  24. android:layout_weight="1"/>
  25. <Button
  26. android:id="@+id/mGoCamera_btn"
  27. android:text="拍照选择"
  28. android:layout_marginBottom="5dp"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content" />
  31. <Button
  32. android:id="@+id/mGoAlbm_btn"
  33. android:text="本地相册选择"
  34. android:layout_marginBottom="10dp"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content" />
  37. </LinearLayout>

本文还有多处可优化的地方,比如Android 6.0以上动态权限的获取、保存图片前对SD卡是否可用的判断、容错措施等等。这些都是不属于本文的主要内容,本文就不再多说了。 其实还有一些偏门的方法,不过本人是不提倡的,比如下文自己处理StrictMode严苛模式、或者将targetSdkVersion改为24以下等方法,这些都是违背开发思想的方法,最好不要使用。

 
 
  1. //建议在application 的onCreate()的方法中调用
  2. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  3. StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
  4. StrictMode.setVmPolicy(builder.build());
  5. }

猜你喜欢

转载自blog.csdn.net/zjy_android/article/details/79586416