如何从缓存或外部存储加载先前下载的pdf

时间:2018-09-04 00:32:57

标签: pdf caching picasso downloading android-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();






    }
}
}

`我正在尝试构建一个带有按钮的应用程序,每个按钮都下载一本书,但是我想要的是单击按钮仅第一次下载书籍。然后,如果用户在一段时间后再次单击以完成读取,请从文件或缓存中加载它,而不是再次重新下载所有内容。

1 个答案:

答案 0 :(得分:0)

单击按钮时,运行一个函数来检查文件是否首先存在,如下所示:

public boolean fileExists(String filename){
    File f= getBaseContext().getFileStreamPath(filename);
    return f.exists();
}

如果函数返回true,那么您知道用户之前已经下载了该文件。只需使用该文件即可,而不是运行下载。