0

我的代码在 Lollipop 设备中运行良好,但在 Nougat 中没有执行任何操作

private ArrayList<PDFDoc> getPDFs() {
    File downloadsFolder=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    if (downloadsFolder.exists()) {
        //GET ALL FILES IN DOWNLOAD FOLDER
        File[] files = downloadsFolder.listFiles();

        //LOOP THRU THOSE FILES GETTING NAME AND URI
        for (int i = 0; i < files.length; i++) {
            File file = files[i];

            if (file.getPath().endsWith("pdf")) {
                pdfDoc = new PDFDoc();
                pdfDoc.setName(file.getName());
                pdfDoc.setPath(file.getAbsolutePath());

                pdfDocs.add(pdfDoc);
            }

        }
    }

    return pdfDocs;
}

这个块在棒棒糖中运行良好,但在牛轧糖中没有,请帮助我进行真正的更改或添加。

4

3 回答 3

1

在调用 getPDFs() 方法之前,您需要检查用户是否授予了存储权限。在 android marshmallow 中,仅在 Manifest 文件中定义权限是行不通的,您需要在运行时请求权限。

所以首先你需要检查运行应用程序的设备是否是棉花糖或以上。您可以使用此代码来完成。

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
  //ask for permission here 
}

要询问运行时权限,您可以使用以下代码。

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    } else {
        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
} else {
    // Permission has already been granted
}

您必须为存储权限执行此操作。我从 Android 开发者页面复制了上面的代码。

有关更多详细信息,请查看请求应用程序的许可

于 2018-08-09T11:29:30.783 回答
0

应用常量

public static final String CAMERA_PERMISSION = CAMERA;
public static final String READ_STORAGE_PERMISSION = READ_EXTERNAL_STORAGE;
public static final String WRITE_STORAGE_PERMISSION = WRITE_EXTERNAL_STORAGE;
public static final String LOCATION_PERMISSION = ACCESS_FINE_LOCATION;
public static final String READ_PHONE_STATE_PERMISSION= READ_PHONE_STATE;

从您的(活动或片段或按钮)调用此函数以检查权限

  //Getting permission here
public boolean needPermission() {
    try {
        int readPhoneState = ContextCompat.checkSelfPermission(this, AppConstants.READ_PHONE_STATE_PERMISSION);
        int location = ContextCompat.checkSelfPermission(this, AppConstants.LOCATION_PERMISSION);
        if (readPhoneState == PackageManager.PERMISSION_GRANTED && location == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            if (readPhoneState != PackageManager.PERMISSION_GRANTED) {
                AppGlobal.checkSinglePermission(this, AppConstants.READ_PHONE_STATE_PERMISSION);
            } else if (location != PackageManager.PERMISSION_GRANTED) {
                AppGlobal.checkSinglePermission(this, AppConstants.LOCATION_PERMISSION);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

此功能将检查权限类型

  //Check single permission
public static boolean checkSinglePermission(final Context context, String permissionType) {
    ArrayList<String> permissionList = new ArrayList<>();
    String type = "";

    if (permissionType != null) {
        if (permissionType.equals(AppConstants.CAMERA_PERMISSION)) {
            type = permissionType;
        }
        if (permissionType.equals(AppConstants.READ_STORAGE_PERMISSION)) {
            type = permissionType;
        }
        if (permissionType.equals(AppConstants.WRITE_STORAGE_PERMISSION)) {
            type = permissionType;
        }
        if (permissionType.equals(AppConstants.READ_PHONE_STATE_PERMISSION)) {
            type = permissionType;
        }
        if (permissionType.equals(AppConstants.LOCATION_PERMISSION)) {
            type = permissionType;
        }

        int getPermission = ContextCompat.checkSelfPermission(context, type);

        if (getPermission != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(type);
            if (!permissionList.isEmpty()) {
                ActivityCompat.requestPermissions((Activity) context, permissionList.toArray
                        (new String[permissionList.size()]), SINGLE_PERMISSION_REQUEST);
                return false;
            }
        }

    }
    return true;
}

然后你会在这个onRequestPermissionsResult()中得到权限成功和失败的响应

 //Permission request result here
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    for (String permission : permissions) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
            AppLog.e("denied" + permission);//denied
            needPermission();//needed permission for getting Country code

        } else {
            if (ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {
                AppLog.e("allowed" + permission);//allowed
                needPermission();//set data
            } else {
                //set to never ask again
                AppLog.e("set to never ask again" + permission);
                AppDialog.showAlertDialog(this, null,
                        getString(R.string.read_phone_state_permission_txt), getString(R.string.txt_ok), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                                AppGlobal.permissionSettingView(LoginActivity.this);

                            }
                        });

            }
        }

    }
}
于 2018-08-09T11:36:35.487 回答
0

为此,您需要添加运行时权限

private static final int REQUEST_WRITE_PERMISSION = 786;

  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {            
        //call func here
    }
  }     

private void requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
        } else {
            //call func here
        }
      }
于 2018-08-09T11:24:25.810 回答