I am working on an android app that should write to a local file on device. A simple txt file. But i want to do this via the web view + javascript feature. In my activity I have a button which points to a URL. When user taps it, it requests a json file. I want to write this blob to disk in the javascript part of the app itself. Is there a way to do this ?
我正在开发一个应该写入设备上本地文件的Android应用程序。一个简单的txt文件。但我想通过网络视图+ javascript功能这样做。在我的活动中,我有一个指向URL的按钮。当用户点击它时,它会请求一个json文件。我想在应用程序本身的javascript部分将此blob写入磁盘。有没有办法做到这一点 ?
1 个解决方案
#1
0
I'm not quite sure what you mean by the javascript part. If you are in an Android app, you should have access to a Context. If you have any Context, you can do IO:
我不太确定你的javascript部分是什么意思。如果您使用的是Android应用,则应该可以访问上下文。如果你有任何Context,你可以做IO:
private String read(String fn)
{
try {
FileInputStream mInput = {{CONTEXT}}.openFileInput(fn);
byte[] data = new byte[128];
mInput.read(data);
mInput.close();
String display = new String(data);
return display.trim();
} catch (IOException | NullPointerException e) {
e.printStackTrace();
return ERROR;
}
}
private void write(String fn, String data)
{
try {
FileOutputStream mOutput = {{CONTEXT}}.openFileOutput(fn);
mOutput.write(data.getBytes());
mOutput.flush();
mOutput.close();
} catch (IOException | NullPointerException e) {
e.printStackTrace();
}
}
#1
0
I'm not quite sure what you mean by the javascript part. If you are in an Android app, you should have access to a Context. If you have any Context, you can do IO:
我不太确定你的javascript部分是什么意思。如果您使用的是Android应用,则应该可以访问上下文。如果你有任何Context,你可以做IO:
private String read(String fn)
{
try {
FileInputStream mInput = {{CONTEXT}}.openFileInput(fn);
byte[] data = new byte[128];
mInput.read(data);
mInput.close();
String display = new String(data);
return display.trim();
} catch (IOException | NullPointerException e) {
e.printStackTrace();
return ERROR;
}
}
private void write(String fn, String data)
{
try {
FileOutputStream mOutput = {{CONTEXT}}.openFileOutput(fn);
mOutput.write(data.getBytes());
mOutput.flush();
mOutput.close();
} catch (IOException | NullPointerException e) {
e.printStackTrace();
}
}