使用weiXinRecorded不支援targetSdkVersion升級23及以上問題的解決方案

Android_feng發表於2019-04-12
  • 1.最近維護的專案中用到了 weiXinRecorded圖片選擇,導致專案targetSdkVersion不能升級到23及以 上

  • 2.在Android 9.0出來之後,對儲存及拍照許可權,更改為了:允許,詢問,拒絕,比之前的允許,拒絕,多了一個詢問的狀態,就必須要用到動態請求許可權, 相矛盾的是 動態許可權是api 23 android 6.0以後的特性,專案為22及以下

  • 3.結局辦法:谷歌官方針對,這一特殊情況,也做了相應的解決方案: 在app moudle 的build.gradle 中新增: 注意:是V4包中

    implementation 'com.android.support:support-v4:support-core-utils-25.1.0'

    implementation 'com.android.support:support-v4:support-compat-25.1.0'

  • 在對應類中需要使用的地方 呼叫(如點選事件,跳轉頁面之前等)

  • 獲取相應許可權

    private void getPermissions() {

    int permission = PermissionChecker.checkSelfPermission(context,
              android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
              
      int cemerms = PermissionChecker.checkSelfPermission(context,
              android.Manifest.permission.CAMERA);
    //0成功  -1失敗
      if (permission == PermissionChecker.PERMISSION_GRANTED && cemerms == PermissionChecker.PERMISSION_GRANTED) {
    
    
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              if (checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager
                      .PERMISSION_GRANTED ||
                      checkSelfPermission(getActivity(), Manifest.permission.RECORD_AUDIO) != PackageManager
                              .PERMISSION_GRANTED ||
                      checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager
                              .PERMISSION_GRANTED ||
                      checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager
                              .PERMISSION_GRANTED) {
    
                  //不具有獲取許可權,需要進行許可權申請
                  ActivityCompat.requestPermissions(getActivity(), new String[]{
                          Manifest.permission.WRITE_EXTERNAL_STORAGE,
                          Manifest.permission.CAMERA,
                          Manifest.permission.RECORD_AUDIO,
                          Manifest.permission.READ_EXTERNAL_STORAGE}, GET_PERMISSION_REQUEST);
    
              } else {
                  startActivity(new Intent(getActivity(), LoginAcitivty.class));
    
              }
          } else {
              startActivity(new Intent(getActivity(), LoginAcitivty.class));
          }
      }else{
      
      
      
          //寫個彈出框,讓使用者去手動開啟許可權
          
          toSelfSetting(MyApplication.getContext());
          
          
      }
      
      <!--跳往應用設定頁面-->
      public static void toSelfSetting(Context context) {
      
      Intent mIntent = new Intent();
      mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      if (Build.VERSION.SDK_INT >= 9) {
          mIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
          mIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
      } else if (Build.VERSION.SDK_INT <= 8) {
          mIntent.setAction(Intent.ACTION_VIEW);
          mIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
          mIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
      }
      context.startActivity(mIntent);
    複製程式碼

    }

相關文章