Я предлагаю пользователю загрузить файл PDF в хранилище устройства, а затем отправляю уведомление, которое откроет этот файл с использованием того же Uri, который был возвращен в методе OnActivityResult (). Но кажется, что новый Intent не может открыть этот Uri, когда я нажимаю на уведомление, ничего не происходит. Вот код:
private fun firePushNotification() {
val intent: Intent
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent = Intent(Intent.ACTION_VIEW).apply {
data = uri // From OnActivityResult()
type = "application/pdf"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
} else {
intent = Intent(Intent.ACTION_VIEW).apply {
data = uri
type = "application/pdf"
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
}
// Push logic
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
val builder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle("Download concluído")
.setContentText("Abrir PDF")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(CHANNEL_ID, "Channel 1",
NotificationManager.IMPORTANCE_DEFAULT).apply {
description = ""
}
// Register the channel with the system
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, builder.build())
}
Я также пытался создать файл, используя этот URI с этим кодом:
private fun firePushNotification() {
val intent: Intent
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val input = context.contentResolver.openInputStream(uri)
val file = File(context.cacheDir.absolutePath, pdfName)
val output = FileOutputStream(file)
copyStream(input!!, output)
intent = Intent(Intent.ACTION_VIEW).apply {
data = FileProvider.getUriForFile(context, context.packageName + ".provider", file)
type = "application/pdf"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
} else {
intent = Intent(Intent.ACTION_VIEW).apply {
data = uri
type = "application/pdf"
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
}
// Push logic ...
}
copystream()
метод:
@Throws(IOException::class)
fun copyStream(inp: InputStream, out: OutputStream) {
val buffer = ByteArray(1024)
var read: Int
while (inp.read(buffer).also { read = it } != -1) {
out.write(buffer, 0, read)
}
}
Есть ли другой способ открыть недавно загруженный файл, используя uri, предоставленный пользовательским вводом?