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

问题描述 投票:-1回答:1
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();






    }
}
}

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

pdf caching picasso downloading android-external-storage
1个回答
0
投票

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

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

如果函数返回true,则表示用户之前已下载该文件。只需使用该文件而不是运行下载。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.