我正在开发一种图书阅读器应用程序,用户可以在epub format
上阅读其图书。
成功下载后,我正在使用folioReader库来存放书籍。
这是我下载每本书的方式(效果很好)
private void downloadFile(final String fileName){
@SuppressLint("StaticFieldLeak") AsyncTask<Void, Integer, Boolean> asyncTask = new AsyncTask<Void, Integer, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
return downloadFile();
}
@NonNull
private Boolean downloadFile(){
try {
file = getFileStreamPath(fileName);
if (file.exists()) {
button_text.setText("READ");
return true;
}
try {
FileOutputStream fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
URL url = new URL(document);
URLConnection urlConnection = url.openConnection();
int contentLength = urlConnection.getContentLength();
InputStream inputStream = new BufferedInputStream(url.openStream());
byte[] data = new byte[contentLength];
long total = 0;
int count;
while ((count = inputStream.read(data)) != -1) {
total += count;
publishProgress((int) ((total * 100) / contentLength));
fileOutputStream.write(data, 0, count);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
return true;
} catch (final Exception e) {
e.printStackTrace();
return false; //swallow a 404
}
}catch (Exception e){
e.printStackTrace();
Toast.makeText(BookDetailsActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
seekBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if(aBoolean){
button_text.setText("READ");
} else {
button_text.setText("DOWNLOAD");
}
downloadProgress.setVisibility(View.GONE);
}
};
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
但是,我无法成功读取/检索保存到内部存储器中的文档的位置。我的错误不断告诉我该路径无效。这是我阅读文档的代码。(不起作用)
try{
File file = getFileStreamPath(title);
folioReader.openBook(file.toString());
Log.e("file: ", file.toString());
} catch (Exception e){
e.printStackTrace();
}
}