我的应用程序从互联网上下载文件并将其保存到“下载”文件夹中,并提供通知,该文件已下载。当我单击通知时,我创建了chooserIntent来选择要打开文件的应用程序。但是,我选择的任何应用程序都不会打开文件,并显示错误消息:“无法打开文件”,但是当我转到文件管理器时,它打开正常。我想这是与权限相关的东西? BTW,Uri是这样获得的(Android 7.1):
Uri fileUri = FileProvider.getUriForFile(getActivityContext(), getAppContext().getPackageName() + ".provider", file);
以下是我的通知代码:
private Notification downloadCompleteNotification(String title, String message, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(uri, "*/*");
Intent chooser = Intent.createChooser(intent, getResources().getString(R.string.open_file_with));
NotificationCompat.Builder builder = initBasicBuilder(title, message, intent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_u));
builder.setOngoing(false);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, chooser, 0);
builder.setContentIntent(pendingIntent);
return builder.build();
}
private NotificationCompat.Builder initBasicBuilder(String title, String text, Intent intent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.u)
.setContentTitle(title)
.setContentText(text);
if (intent != null) {
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addNextIntentWithParentStack(intent);
}
return builder;
}
答案 0 :(得分:0)
将此代码放入onCreate方法活动中。您可以在此处添加以逗号分隔的多个权限
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE};
if(!hasPermissions(this, PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
并添加此功能:
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
答案 1 :(得分:0)
如果您使用的是API level 23
或以上。您需要授予Run Time
权限。以及清单文件内部。
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private boolean checkPermissions() {
int result;
List<String> listPermissionsNeeded = new ArrayList<>();
for (String p : PERMISSIONS_STORAGE) {
result = ContextCompat.checkSelfPermission(mContext, p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_EXTERNAL_STORAGE);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted.
} else {
//code for deny
}
break;
}
}