我有一个可以在Lollipop设备中正常运行的代码,但是在牛轧糖中却什么也没做
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;
}
在棒棒糖中此块运行正常,但效果理想,但在牛轧糖中却不能,请帮助我进行真正的更改或添加。
答案 0 :(得分:1)
在调用方法getPDFs()之前,您需要检查存储权限是否由用户授予。 从android棉花糖,仅在清单文件中定义权限将不起作用,您需要在运行时请求权限。
因此,首先您需要检查运行该应用程序的设备是否为棉花糖或以上。您可以使用此代码来实现。
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开发人员页面复制了上面的代码。
有关更多详细信息,请检查Requesting App's permission
答案 1 :(得分: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
}
}
答案 2 :(得分:0)
AppConstants
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);
}
});
}
}
}
}