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

  • 1.最近維護的項目中用到了 weiXinRecorded圖片選擇,致使項目targetSdkVersion不能升級到23及以 上android

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

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

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

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

  • 在對應類中須要使用的地方 調用(如點擊事件,跳轉頁面以前等)spa

  • 獲取相應權限code

    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);
    複製代碼

    }圖片

相關文章
相關標籤/搜索