如何在应用程序中保存.HTML页面/文件';android中的s数据库?
我现在正在开发一个应用程序,它可以作为一个书堆,用户可以阅读他们选择的书籍,现在我正在做的是,在我的应用程序的web视图中显示我制作的html页面。 现在,只有当用户的手机上有全天候的互联网连接时,这个应用程序才能工作。 我真正想要的是,当他们第一次打开应用程序时,他们需要互联网连接,然后应用程序应该能够下载该页面并将其存储在本地数据库中,这样用户以后就可以阅读该页面,而无需任何互联网连接。 那么,是否有任何可能的方法下载html页面并将其存储在本地数据库中,以便用户即使未连接到internet也可以使用该应用程序? 如果需要,我可以在这里发布我的代码。 任何最小的提示或帮助都会非常好,因为我已经被困在这里很久了:( 编辑1:如何在应用程序中保存.HTML页面/文件';android中的s数据库?,android,html,sqlite,Android,Html,Sqlite,我现在正在开发一个应用程序,它可以作为一个书堆,用户可以阅读他们选择的书籍,现在我正在做的是,在我的应用程序的web视图中显示我制作的html页面。 现在,只有当用户的手机上有全天候的互联网连接时,这个应用程序才能工作。 我真正想要的是,当他们第一次打开应用程序时,他们需要互联网连接,然后应用程序应该能够下载该页面并将其存储在本地数据库中,这样用户以后就可以阅读该页面,而无需任何互联网连接。 那么,是否有任何可能的方法下载html页面并将其存储在本地数据库中,以便用户即使未连接到internet
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
因此,我成功地从网站下载了HTLM页面,但现在我面临的问题是,我看不到下载的html的任何图像。有什么合适的解决方案?这里是“本地数据库”的意思
首选方法是在内部存储器中下载网页
(/用于下载网页的代码片段。检查代码中的注释。只需提供链接,即www.mytestpage.com/story1.htm作为该函数的下载链接
void Download(String downloadlink,int choice)
{
try {
String USERAGENT;
if(choice==0)
{
USERAGENT ="Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Safari/530.17";
}
else
{
USERAGENT ="Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; ADR6300 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17";
}
URL url = new URL(downloadlink);
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestProperty("User-Agent", USERAGENT); //if you are not sure of user agent just set choice=0
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
//set the path where we want to save the file
File SDCardRoot = Environment.getExternalStorageDirectory();
File dir = new File (SDCardRoot.getAbsolutePath() + "/yourfolder");
if(!dir.exists())
{
dir.mkdirs();
}
File file = new File(dir, "filename"); //any name abc.html
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
}
//close the output stream when done
fileOutput.close();
inputStream.close();
urlConnection.disconnect();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
使用文件保存下载的内容。是的,我很想这样做,但是如何下载一个网页?就像我想下载www.mytestpage.com/story1.htm一样。你能发布一个示例代码吗?我在这个领域还是初学者,非常感谢你的帮助。:)好的,我成功下载了这个页面,但现在我面临的问题是,我无法在脱机模式下看到图像,有什么解决方案吗?因为图像是我应用程序中最重要的部分。@我面临的问题是,你有什么与此问题相关的解决方案吗?我如何使用截击来解决这个问题?好的,我下载了这个页面成功,但现在我面临的问题是,我无法在脱机模式下查看图像,是否有解决此问题的方法?因为图像是我的应用程序最重要的部分。问题是,无论何时尝试从url下载,都会下载html页面而不是图像。为了完成此任务,您将要么必须搜索某个执行此操作的库,要么我的解决方案将涉及使用htmlparser
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
void Download(String downloadlink,int choice)
{
try {
String USERAGENT;
if(choice==0)
{
USERAGENT ="Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Safari/530.17";
}
else
{
USERAGENT ="Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; ADR6300 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17";
}
URL url = new URL(downloadlink);
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestProperty("User-Agent", USERAGENT); //if you are not sure of user agent just set choice=0
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
//set the path where we want to save the file
File SDCardRoot = Environment.getExternalStorageDirectory();
File dir = new File (SDCardRoot.getAbsolutePath() + "/yourfolder");
if(!dir.exists())
{
dir.mkdirs();
}
File file = new File(dir, "filename"); //any name abc.html
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
}
//close the output stream when done
fileOutput.close();
inputStream.close();
urlConnection.disconnect();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}