如何从缓存或外部存储加载以前下载的 pdf
How to load previously downloaded pdf from cache or external storage
public class physics extends AppCompatActivity {
PDFView pdfViewph;
ImageView image;
ProgressBar progressbar;
TextView ans4 = (TextView) findViewById(R.id.ans4);
// Set a click listener on that View
ans4.setOnClickListener(new View.OnClickListener() {
category is clicked on.
@Override
public void onClick(View view) {
new RetrievePDFBytes().execute("https://docs.google.com/uc?authuser=0&id=0B7aQiU7nV3LranpkTi1FZ2hOZmc&export=download");
}
});}
class RetrievePDFBytes extends AsyncTask<String,Void,byte[]> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressbar = (ProgressBar)findViewById(R.id.ProgressBar);
progressbar.setVisibility(View.VISIBLE);
}
@Override
protected byte[] doInBackground(String... strings) {
InputStream inputStream = null;
try{
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
if(urlConnection.getResponseCode() == 200)
{
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
}
catch (IOException e)
{
return null;
}
try {
return IOUtils.toByteArray(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(byte[] bytes) {
progressbar.setVisibility(View.INVISIBLE);;
pdfViewph.setVisibility(View.VISIBLE);
pdfViewph.fromBytes(bytes).load();
}
}
}
`我正在尝试构建一个应用程序,它有一个按钮,每个按钮都会下载一本书,但我想要的是点击按钮,它只在第一次下载一本书。然后,如果用户在一段时间后再次单击以完成阅读,则从文件或缓存中加载它,而不是再次重新下载。
点击按钮后,运行首先检查文件是否存在的函数,如下所示:
public boolean fileExists(String filename){
File f= getBaseContext().getFileStreamPath(filename);
return f.exists();
}
如果函数 returns 为真,那么您就知道用户之前已经下载过该文件。只需使用该文件而不是 运行 下载。
public class physics extends AppCompatActivity {
PDFView pdfViewph;
ImageView image;
ProgressBar progressbar;
TextView ans4 = (TextView) findViewById(R.id.ans4);
// Set a click listener on that View
ans4.setOnClickListener(new View.OnClickListener() {
category is clicked on.
@Override
public void onClick(View view) {
new RetrievePDFBytes().execute("https://docs.google.com/uc?authuser=0&id=0B7aQiU7nV3LranpkTi1FZ2hOZmc&export=download");
}
});}
class RetrievePDFBytes extends AsyncTask<String,Void,byte[]> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressbar = (ProgressBar)findViewById(R.id.ProgressBar);
progressbar.setVisibility(View.VISIBLE);
}
@Override
protected byte[] doInBackground(String... strings) {
InputStream inputStream = null;
try{
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
if(urlConnection.getResponseCode() == 200)
{
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
}
catch (IOException e)
{
return null;
}
try {
return IOUtils.toByteArray(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(byte[] bytes) {
progressbar.setVisibility(View.INVISIBLE);;
pdfViewph.setVisibility(View.VISIBLE);
pdfViewph.fromBytes(bytes).load();
}
}
}
`我正在尝试构建一个应用程序,它有一个按钮,每个按钮都会下载一本书,但我想要的是点击按钮,它只在第一次下载一本书。然后,如果用户在一段时间后再次单击以完成阅读,则从文件或缓存中加载它,而不是再次重新下载。
点击按钮后,运行首先检查文件是否存在的函数,如下所示:
public boolean fileExists(String filename){ File f= getBaseContext().getFileStreamPath(filename); return f.exists(); }
如果函数 returns 为真,那么您就知道用户之前已经下载过该文件。只需使用该文件而不是 运行 下载。