Skip to content

Commit d750eb6

Browse files
committed
has circumvented the bug while reading some mdx file
1 parent b36f1db commit d750eb6

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

Diff for: app/src/main/java/com/example/mdictjnilibrary/MainActivity.java

+70-7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
import androidx.appcompat.app.AppCompatActivity;
44
import android.os.Bundle;
5+
import android.os.Environment;
6+
import android.util.Log;
57
import android.webkit.WebSettings;
68
import android.webkit.WebView;
9+
import android.widget.Toast;
710

811
import java.io.File;
9-
12+
import java.io.FileNotFoundException;
13+
import java.io.FileOutputStream;
14+
import java.io.IOException;
1015

1116

1217
public class MainActivity extends AppCompatActivity {
13-
String path = "German Conjugation(185 verbs).mdx";
18+
String dict = "German Conjugation(185 verbs).mdx";
1419
String word = "sein";
1520

1621
WebView webView;
@@ -25,14 +30,72 @@ protected void onCreate(Bundle savedInstanceState) {
2530

2631
setContentView(R.layout.activity_main);
2732
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);
3036

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+
}
3243

3344
// 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+
}
3699
}
37100

38101
/**

0 commit comments

Comments
 (0)