I have face one issue when opening Pdf file in my project
in my project i'm implemented PDF opening with notification type,while opening downloaded PDF file it shows blank page
In manifeast file:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/path_provider"
tools:replace="android:resource"/>
</provider>
path_provider:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
public void customNotifcation1() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
File file = new File(getActivity().getFilesDir(), pdfFileName);
String absoluteFilePath = file.getAbsolutePath();
Uri uri = Uri.parse("content://" +
getActivity().getPackageName() + "/" + absoluteFilePath);
PendingIntent pendingIntent = null;
CharSequence name = "Attendance";
String description = uri + "";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new
NotificationChannel("Attendance", name, importance);
channel.setDescription(description);
NotificationManager notificationManager =
getActivity().getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder notification = new
NotificationCompat.Builder(getActivity(), "Attendance")
.setContentTitle(name)
.setContentText(description)
.setAutoCancel(true)
.setSmallIcon(R.drawable.icon50);
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent = Intent.createChooser(intent, "Choose Pdf
Application");
//pendingIntent = PendingIntent.getActivity(getActivity(),
0, intent, 0);
pendingIntent = PendingIntent.getActivity(getActivity(), 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "No Application Available to
fiew this file type", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Log.e("Exception", e.toString());
}
notification.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.icon));
notification.setContentIntent(pendingIntent);
notificationManager.notify(1, notification.build());
} else {
PendingIntent pendingIntent = null;
File file = null;
NotificationCompat.Builder builder = new
NotificationCompat.Builder(getActivity());
builder.setSmallIcon(R.drawable.icon50);
try {
file = new
File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + pdfFileName);
file = new File(getActivity().getFilesDir(), pdfFileName);
String absoluteFilePath = file.getAbsolutePath();
Uri uri = Uri.parse("content://" +
getActivity().getPackageName() + "/" + absoluteFilePath);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setDataAndType(Uri.fromFile(file),
"application/pdf");
// intent = Intent.createChooser(intent, "Open File");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
pendingIntent = PendingIntent.getActivity(getActivity(), 0,
intent, 0);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "No Application Available to
fiew this file type", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Log.e("Exception", e.toString());
}
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.icon));
builder.setContentTitle("Attendance");
builder.setContentText(file + "");
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)
getActivity().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
}
});
}
from While Open PDF file in android N it shows Black color blank Page
No comments:
Post a Comment