当前位置:   article > 正文

android调用相机获取图片程序全代码,附Unable to get provider android.support.v4.content.FileProvider和其他一些坑最简单有效的解决办法_unable to get provider androidx.core.content.filep

unable to get provider androidx.core.content.fileprovider

经过一个小时的不断试错,发现网上说的什么mutiDex那些简直是瞎扯。

真正的解决办法是在AndroidManifest.xml中的provider的android:name设置为"androidx.core.content.FileProvider",不要再傻傻的去加什么compile鬼东西了。

 

下面附上一个调用相机获取图片的完整可用程序。

界面布局:一个ImageView和一个Button。只有一个Activity叫MainActivity。

在res文件夹下创建一个xml文件夹,里面创建一个名字叫file_paths.xml的文件

file_paths.xml

注意:

如果图片位置是在getFilesDir()下任意位置的话(我这个程序就是),节点类型就是files-path

如果图片位置是在getFilesDir()下任意位置的话,节点类型就是external-path

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <paths xmlns:android="http://schemas.android.com/apk/res/android">
  3. <files-path name="external_files" path="." />
  4. </paths>

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.cyy.camera">
  4. <uses-permission android:name="android.permission.CAMERA"/>
  5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  6. <uses-permission android:name="android.permission.INTERNET"/>
  7. <application
  8. android:allowBackup="true"
  9. android:icon="@mipmap/ic_launcher"
  10. android:label="@string/app_name"
  11. android:roundIcon="@mipmap/ic_launcher_round"
  12. android:supportsRtl="true"
  13. android:theme="@style/AppTheme">
  14. <activity android:name=".MainActivity">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17. <category android:name="android.intent.category.LAUNCHER" />
  18. </intent-filter>
  19. </activity>
  20. <provider
  21. android:name="androidx.core.content.FileProvider"
  22. android:authorities="com.cyy.camera.provider"
  23. android:exported="false"
  24. android:grantUriPermissions="true">
  25. <meta-data
  26. android:name="android.support.FILE_PROVIDER_PATHS"
  27. android:resource="@xml/file_paths">
  28. </meta-data>
  29. </provider>
  30. </application>
  31. </manifest>

MainActivity.java

  1. package com.cyy.camera;
  2. import android.Manifest;
  3. import android.app.AlertDialog;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.provider.MediaStore;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.ImageView;
  14. import android.widget.Toast;
  15. import androidx.annotation.NonNull;
  16. import androidx.annotation.Nullable;
  17. import androidx.appcompat.app.AppCompatActivity;
  18. import androidx.core.app.ActivityCompat;
  19. import androidx.core.content.ContextCompat;
  20. import androidx.core.content.FileProvider;
  21. import java.io.File;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import static androidx.core.content.PermissionChecker.PERMISSION_GRANTED;
  25. public class MainActivity extends AppCompatActivity {
  26. private ImageView imageView;
  27. private static int GET_PHOTO=0;
  28. private Uri fileUri;
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. Button button = findViewById(R.id.button);
  34. button.setOnClickListener(new TakePhoto());
  35. imageView=findViewById(R.id.imageView);
  36. RequestPermission();
  37. }
  38. private void RequestPermission(){
  39. String[] permissions=new String[]{
  40. Manifest.permission.CAMERA,
  41. Manifest.permission.WRITE_EXTERNAL_STORAGE,
  42. Manifest.permission.INTERNET,
  43. Manifest.permission.READ_EXTERNAL_STORAGE
  44. };
  45. for (String permission : permissions) {
  46. if ((ContextCompat.checkSelfPermission(this, permission) != PERMISSION_GRANTED)) {
  47. ActivityCompat.requestPermissions(this, permissions, 0);
  48. }
  49. }
  50. }
  51. @Override
  52. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  53. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  54. for (int i=0;i<permissions.length;i++){
  55. String permission=permissions[i];
  56. if(!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,permission)){
  57. AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
  58. builder.setTitle("提醒");
  59. builder.setMessage("请去往设置页为本程序授予权限,否则将无法使用");
  60. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  61. @Override
  62. public void onClick(DialogInterface dialog, int which) {
  63. System.exit(0);
  64. }
  65. });
  66. AlertDialog alertDialog=builder.create();
  67. alertDialog.setCanceledOnTouchOutside(false);
  68. return;
  69. }
  70. else if(grantResults[i]!=PERMISSION_GRANTED){
  71. AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
  72. builder.setTitle("提醒");
  73. builder.setMessage("请授予权限,否则将无法使用程序");
  74. builder.setPositiveButton("确定",null);
  75. builder.create().show();
  76. return;
  77. }
  78. }
  79. }
  80. private class TakePhoto implements View.OnClickListener{
  81. @Override
  82. public void onClick(View v) {
  83. File imageFile=new File(getFilesDir(),"CameraApp/abc.jpg");
  84. try {
  85. File dir=new File(getFilesDir(),"CameraApp");
  86. if(!dir.exists()){
  87. if(!dir.mkdirs()){
  88. Toast.makeText(MainActivity.this, "无法创建缓存文件", Toast.LENGTH_LONG).show();
  89. return;
  90. }
  91. }
  92. Toast.makeText(MainActivity.this,dir.toString(),Toast.LENGTH_LONG).show();
  93. while(!imageFile.createNewFile()){
  94. if(!imageFile.delete()){
  95. Toast.makeText(MainActivity.this, "无法创建缓存文件", Toast.LENGTH_LONG).show();
  96. return;
  97. }
  98. }
  99. Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
  100. fileUri= FileProvider.getUriForFile(MainActivity.this,"com.cyy.camera.provider",imageFile);
  101. intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
  102. startActivityForResult(intent,GET_PHOTO);
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. }
  108. @Override
  109. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  110. if (requestCode == GET_PHOTO) {
  111. try {
  112. Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
  113. imageView.setImageBitmap(bitmap);
  114. } catch (FileNotFoundException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. super.onActivityResult(requestCode, resultCode, data);
  119. }
  120. }

谷歌越更新越复杂,适配的版本越多,真希望统一一下标准,以后就别改了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/175370
推荐阅读
相关标签
  

闽ICP备14008679号