2
2
3
3
import androidx .appcompat .app .AppCompatActivity ;
4
4
import android .os .Bundle ;
5
+ import android .os .Environment ;
6
+ import android .util .Log ;
5
7
import android .webkit .WebSettings ;
6
8
import android .webkit .WebView ;
9
+ import android .widget .Toast ;
7
10
8
11
import java .io .File ;
9
-
12
+ import java .io .FileNotFoundException ;
13
+ import java .io .FileOutputStream ;
14
+ import java .io .IOException ;
10
15
11
16
12
17
public class MainActivity extends AppCompatActivity {
13
- String path = "German Conjugation(185 verbs).mdx" ;
18
+ String dict = "German Conjugation(185 verbs).mdx" ;
14
19
String word = "sein" ;
15
20
16
21
WebView webView ;
@@ -25,14 +30,72 @@ protected void onCreate(Bundle savedInstanceState) {
25
30
26
31
setContentView (R .layout .activity_main );
27
32
webView = findViewById (R .id .myWebView );
28
- WebSettings settings = webView .getSettings ();
29
- settings .setDefaultTextEncodingName ("utf-8" );
33
+ webView .getSettings ().setAllowFileAccess (true );
34
+
35
+ File dictionary = new File (getExternalFilesDir (null ), dict );
30
36
31
- File dict = new File (getExternalFilesDir (null ), path );
37
+ if (dictionary .exists ()) {
38
+ Log .i ("FileInfo" , "the absolute path of the parent directory is " + getExternalFilesDir (null ).getAbsolutePath ());
39
+ Log .i ("FileInfo" , "the absolute path of the file is " + dictionary .getAbsolutePath ());
40
+ } else {
41
+ Log .i ("FileInfo" , "the file cannot be found" );
42
+ }
32
43
33
44
// Example of a call to a native method
34
- String queryReturnedValue = entryPoint (dict .getAbsolutePath (), word );
35
- webView .loadData (queryReturnedValue , "text/html; charset=utf-8" , "UTF-8" );
45
+ String queryReturnedValue = entryPoint (dictionary .getAbsolutePath (), word );
46
+ writeFile (queryReturnedValue );
47
+
48
+ File dictDirectory = createDictDirectory (dict );
49
+ File html = createHtmlPage (dictDirectory );
50
+ webView .loadUrl (html .getAbsolutePath ());
51
+
52
+ Log .i ("length" , String .valueOf (queryReturnedValue .length ()));
53
+ }
54
+
55
+ private boolean isExternalStorageWritable () {
56
+ if (Environment .MEDIA_MOUNTED .equals (Environment .getExternalStorageState ())) {
57
+ Log .i ("StateInfo" , "Yes, it is writable!" );
58
+ return true ;
59
+ } else {
60
+ return false ;
61
+ }
62
+ }
63
+
64
+ private File createDictDirectory (String dict ) {
65
+ return new File (getExternalFilesDir (null ).getAbsolutePath () + "/" + dict .substring (0 , dict .length () - 4 ));
66
+ }
67
+
68
+ private File createHtmlPage (File dictDirectory ) {
69
+ return new File (dictDirectory , word + ".html" );
70
+ }
71
+
72
+ private void writeFile (String definition ) {
73
+ if (isExternalStorageWritable ()) {
74
+ File dictDirectory = new File (getExternalFilesDir (null ).getAbsolutePath () + "/" + dict .substring (0 , dict .length () - 4 ));
75
+ if (!dictDirectory .exists ()) {
76
+ boolean wasSuccessful = dictDirectory .mkdir ();;
77
+ if (!wasSuccessful ) {
78
+ Log .i ("FileInfo" , "directory creation is not successful" );
79
+ }
80
+ }
81
+ File htmlPage = new File (dictDirectory , word + ".html" );
82
+ if (!htmlPage .exists ()) {
83
+ Log .i ("FileInfo" , "file exist" );
84
+ try {
85
+ FileOutputStream fos = new FileOutputStream (htmlPage );
86
+ fos .write (definition .getBytes ());
87
+ fos .close ();
88
+
89
+ Toast .makeText (this , "saved to" + htmlPage .getAbsolutePath (), Toast .LENGTH_LONG ).show ();
90
+ } catch (FileNotFoundException e ) {
91
+ e .printStackTrace ();
92
+ } catch (IOException e ) {
93
+ e .printStackTrace ();
94
+ }
95
+ }
96
+ } else {
97
+ Toast .makeText (this , "cannot write to external storage" , Toast .LENGTH_LONG ).show ();
98
+ }
36
99
}
37
100
38
101
/**
0 commit comments