@@ -360,18 +360,23 @@ public boolean shareImage(Bitmap bitmap, Bitmap.CompressFormat format, int quali
360
360
*/
361
361
@ RequiresApi (api = Build .VERSION_CODES .KITKAT )
362
362
@ SuppressWarnings ("deprecation" )
363
- public PrintJob print (WebView webview , String jobName ) {
363
+ public PrintJob print (final WebView webview , final String jobName , final boolean ... landscape ) {
364
364
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
365
- PrintDocumentAdapter printAdapter ;
366
- PrintManager printManager = (PrintManager ) _context .getSystemService (Context .PRINT_SERVICE );
365
+ final PrintDocumentAdapter printAdapter ;
366
+ final PrintManager printManager = (PrintManager ) _context .getSystemService (Context .PRINT_SERVICE );
367
367
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
368
368
printAdapter = webview .createPrintDocumentAdapter (jobName );
369
369
} else {
370
370
printAdapter = webview .createPrintDocumentAdapter ();
371
371
}
372
+ final PrintAttributes .Builder attrib = new PrintAttributes .Builder ();
373
+ if (landscape != null && landscape .length > 0 && landscape [0 ]) {
374
+ attrib .setMediaSize (new PrintAttributes .MediaSize ("ISO_A4" , "android" , 11690 , 8270 ));
375
+ attrib .setMinMargins (new PrintAttributes .Margins (0 , 0 , 0 , 0 ));
376
+ }
372
377
if (printManager != null ) {
373
378
try {
374
- return printManager .print (jobName , printAdapter , new PrintAttributes . Builder () .build ());
379
+ return printManager .print (jobName , printAdapter , attrib .build ());
375
380
} catch (Exception ignored ) {
376
381
}
377
382
}
@@ -572,6 +577,14 @@ public File extractFileFromIntent(Intent receivingIntent) {
572
577
}
573
578
}
574
579
580
+ // media/ prefix for External storage
581
+ if (fileStr .startsWith ((tmps = "media/" ))) {
582
+ File f = new File (Uri .decode (Environment .getExternalStorageDirectory ().getAbsolutePath () + "/" + fileStr .substring (tmps .length ())));
583
+ if (f .exists ()) {
584
+ return f ;
585
+ }
586
+ }
587
+
575
588
// Next/OwnCloud Fileprovider
576
589
for (String fp : new String []{"org.nextcloud.files" , "org.nextcloud.beta.files" , "org.owncloud.files" }) {
577
590
if (fileProvider .equals (fp ) && fileStr .startsWith (tmps = "external_files/" )) {
@@ -587,6 +600,16 @@ public File extractFileFromIntent(Intent receivingIntent) {
587
600
return new File (Uri .decode (Environment .getExternalStorageDirectory ().getAbsolutePath () + fileStr .substring (tmps .length ())));
588
601
}
589
602
603
+ if (fileStr .startsWith (tmps = "external_files/" )) {
604
+ for (String prefix : new String []{Environment .getExternalStorageDirectory ().getAbsolutePath (), "/storage" , "" }) {
605
+ File f = new File (Uri .decode (prefix + "/" + fileStr .substring (tmps .length ())));
606
+ if (f .exists ()) {
607
+ return f ;
608
+ }
609
+ }
610
+
611
+ }
612
+
590
613
// URI Encoded paths with full path after content://package/
591
614
if (fileStr .startsWith ("/" ) || fileStr .startsWith ("%2F" )) {
592
615
tmpf = new File (Uri .decode (fileStr ));
@@ -624,6 +647,11 @@ public void requestGalleryPicture() {
624
647
}
625
648
}
626
649
650
+ public String extractFileFromIntentStr (Intent receivingIntent ) {
651
+ File f = extractFileFromIntent (receivingIntent );
652
+ return f != null ? f .getAbsolutePath () : null ;
653
+ }
654
+
627
655
/**
628
656
* Request a picture from camera-like apps
629
657
* Result ({@link String}) will be available from {@link Activity#onActivityResult(int, int, Intent)}.
@@ -647,7 +675,7 @@ public String requestCameraPicture(File target) {
647
675
if (target != null && !target .isDirectory ()) {
648
676
photoFile = target ;
649
677
} else {
650
- SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd'T'HH-mm-ss" , Locale .getDefault () );
678
+ SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd'T'HH-mm-ss" , Locale .ENGLISH );
651
679
File storageDir = target != null ? target : new File (Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DCIM ), "Camera" );
652
680
String imageFileName = ((new ContextUtils (_context ).rstr ("app_name" )).replaceAll ("[^a-zA-Z0-9\\ .\\ -]" , "_" ) + "_" ).replace ("__" , "_" ) + sdf .format (new Date ());
653
681
photoFile = new File (storageDir , imageFileName + ".jpg" );
@@ -717,6 +745,10 @@ public Object extractResultFromActivityResult(int requestCode, int resultCode, I
717
745
cursor .close ();
718
746
}
719
747
748
+ // Try to grab via file extraction method
749
+ data .setAction (Intent .ACTION_VIEW );
750
+ picturePath = picturePath != null ? picturePath : extractFileFromIntentStr (data );
751
+
720
752
// Retrieve image from file descriptor / Cloud, e.g.: Google Drive, Picasa
721
753
if (picturePath == null && Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
722
754
try {
@@ -999,7 +1031,8 @@ private Activity greedyGetActivity(Activity... activity) {
999
1031
public boolean canWriteFile (File file , boolean isDir ) {
1000
1032
if (file == null ) {
1001
1033
return false ;
1002
- } else if (file .getAbsolutePath ().startsWith (Environment .getExternalStorageDirectory ().getAbsolutePath ())) {
1034
+ } else if (file .getAbsolutePath ().startsWith (Environment .getExternalStorageDirectory ().getAbsolutePath ())
1035
+ || file .getAbsolutePath ().startsWith (_context .getFilesDir ().getAbsolutePath ())) {
1003
1036
boolean s1 = isDir && file .getParentFile ().canWrite ();
1004
1037
return !isDir && file .getParentFile () != null ? file .getParentFile ().canWrite () : file .canWrite ();
1005
1038
} else {
@@ -1087,11 +1120,12 @@ public void showMountSdDialog(@StringRes int title, @StringRes int description,
1087
1120
dialogi .show ();
1088
1121
}
1089
1122
1123
+ @ SuppressWarnings ({"ResultOfMethodCallIgnored" , "StatementWithEmptyBody" })
1090
1124
public void writeFile (File file , boolean isDirectory , Callback .a2 <Boolean , FileOutputStream > writeFileCallback ) {
1091
1125
try {
1092
1126
FileOutputStream fileOutputStream = null ;
1093
1127
ParcelFileDescriptor pfd = null ;
1094
- if (file .canWrite ()) {
1128
+ if (file .canWrite () || (! file . exists () && file . getParentFile (). canWrite ()) ) {
1095
1129
if (isDirectory ) {
1096
1130
file .mkdirs ();
1097
1131
} else {
@@ -1112,7 +1146,10 @@ public void writeFile(File file, boolean isDirectory, Callback.a2<Boolean, FileO
1112
1146
writeFileCallback .callback (fileOutputStream != null || (isDirectory && file .exists ()), fileOutputStream );
1113
1147
}
1114
1148
if (fileOutputStream != null ) {
1115
- fileOutputStream .close ();
1149
+ try {
1150
+ fileOutputStream .close ();
1151
+ } catch (Exception ignored ) {
1152
+ }
1116
1153
}
1117
1154
if (pfd != null ) {
1118
1155
pfd .close ();
0 commit comments