1+ package net .webdrop ;
2+
3+ import android .content .ContentResolver ;
4+ import android .content .ContentValues ;
5+ import android .content .Context ;
6+ import android .net .Uri ;
7+ import android .os .Build ;
8+ import android .os .Environment ;
9+ import android .provider .MediaStore ;
10+ import android .util .Base64 ;
11+ import android .webkit .JavascriptInterface ;
12+ import android .widget .Toast ;
13+
14+ import java .io .File ;
15+ import java .io .FileOutputStream ;
16+ import java .io .IOException ;
17+ import java .io .OutputStream ;
18+ import java .text .DateFormat ;
19+ import java .util .Date ;
20+
21+ public class JavaScriptInterface {
22+ private final Context context ;
23+
24+ public JavaScriptInterface (Context context ) {
25+ this .context = context ;
26+ }
27+
28+ @ JavascriptInterface
29+ public void getBase64FromBlobData (String base64Data , String mimeType , String extension ) throws IOException {
30+ convertBase64StringToFileAndSaveIt (base64Data , mimeType , extension );
31+ }
32+
33+ public static String getBase64StringFromBlobUrl (String blobUrl , String mimeType , String extension ) {
34+ if (blobUrl .startsWith ("blob" )) {
35+ return "javascript: var xhr = new XMLHttpRequest();" +
36+ "xhr.open('GET', '" + blobUrl + "', true);" +
37+ "xhr.setRequestHeader('Content-type','" + mimeType + "');" +
38+ "xhr.responseType = 'blob';" +
39+ "xhr.onload = function(e) {" +
40+ " if (this.status == 200) {" +
41+ " var blobPdf = this.response;" +
42+ " var reader = new FileReader();" +
43+ " reader.readAsDataURL(blobPdf);" +
44+ " reader.onloadend = function() {" +
45+ " base64data = reader.result;" +
46+ " Android.getBase64FromBlobData(base64data,'" + mimeType + "','" + extension + "');" +
47+ " }" +
48+ " }" +
49+ "};" +
50+ "xhr.send();" ;
51+ }
52+ return "javascript: console.log('It is not a Blob URL');" ;
53+ }
54+
55+ private void convertBase64StringToFileAndSaveIt (String base64Data , String mimeType , String extension ) throws IOException {
56+ String currentDateTime = DateFormat .getDateTimeInstance ().format (new Date ());
57+ String fileName = "File_" + currentDateTime ;
58+ if (!extension .isEmpty ()) fileName = fileName + "_." + extension ;
59+ byte [] fileAsBytes = Base64 .decode (base64Data .replaceFirst ("^data:" + mimeType + ";base64," , "" ), 0 );
60+
61+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
62+ ContentResolver resolver = context .getContentResolver ();
63+ ContentValues contentValues = new ContentValues ();
64+ contentValues .put (MediaStore .Downloads .DISPLAY_NAME , fileName );
65+ contentValues .put (MediaStore .Downloads .MIME_TYPE , mimeType );
66+ contentValues .put (MediaStore .Downloads .DATE_ADDED , System .currentTimeMillis ());
67+ contentValues .put (MediaStore .Downloads .RELATIVE_PATH , Environment .DIRECTORY_DOWNLOADS );
68+
69+ Uri uri = resolver .insert (MediaStore .Downloads .EXTERNAL_CONTENT_URI , contentValues );
70+ OutputStream outputStream = resolver .openOutputStream (uri );
71+ outputStream .write (fileAsBytes );
72+ outputStream .close ();
73+
74+ } else {
75+ final File file = new File (Environment .getExternalStoragePublicDirectory (
76+ Environment .DIRECTORY_DOWNLOADS ) + "/" + fileName );
77+ FileOutputStream fileOutputStream = new FileOutputStream (file , false );
78+ fileOutputStream .write (fileAsBytes );
79+ fileOutputStream .flush ();
80+ }
81+ Toast .makeText (context , "Check downloads folder ✅" , Toast .LENGTH_SHORT ).show ();
82+ }
83+ }
0 commit comments