1.实现pdf的阅读是一位大神给我说的,

  因为Android不能像ios一样直接内置打开pdf,所以只能通过手机上自带的可以打开pdf的应用,这样也就是通过第三方的应用实现的,主要代码附上:

1》下载当前的文件的公共类

public class Downloader implements Callable {    protected int connectTimeout = 30 * 1000;    protected int readTimeout = 1 * 1000 * 1000;    protected int speedRefreshInterval = 500;    protected byte[] buffer;    private URL url;    private File file;    private float averageSpeed;    private float currentSpeed;    public Downloader() {        buffer = new byte[8 * 1024];    }    public void setUrlAndFile(URL url, File file) {        this.url = url;        this.file = file;        this.averageSpeed = 0;        this.currentSpeed = 0;    }    public URL getUrl() {        return url;    }    public File getFile() {        return file;    }    public float getAverageSpeed() {        return averageSpeed;    }    public float getCurrentSpeed() {        return currentSpeed;    }    @Override    public File call() throws Exception {        StopWatch watch = new StopWatch();        watch.start();        InputStream in = null;        OutputStream out = null;        try {            URLConnection conn = url.openConnection();            conn.setConnectTimeout(connectTimeout);            conn.setReadTimeout(readTimeout);            conn.connect();            in = conn.getInputStream();            out = new FileOutputStream(file);            int time = 0;            int bytesInTime = 0;            for (;;) {                watch.split();                int bytes = in.read(buffer);                if (bytes == -1) {                    break;                }                out.write(buffer, 0, bytes);                time += watch.getTimeFromSplit();                if (time >= speedRefreshInterval) {                    currentSpeed = getSpeed(bytesInTime, time);                    time = 0;                    bytesInTime = 0;                }            }        } catch (IOException e) {            file.delete();            throw e;        } finally {            if (in != null) {                try {                    in.close();                } catch (IOException e) {                }            }            if (out != null) {                try {                    out.flush();                    out.close();                } catch (IOException e) {                }            }        }        watch.stop();        averageSpeed = getSpeed(file.length(), watch.getTime());        return file;    }    private static float getSpeed(long bytesInTime, long time) {        return (float) bytesInTime / 1024 / ((float) time / 1000);    }}

2》调用

private void getPDF() {    PerssionUtils.verifyStoragePermissions(this);    URL url = null;    try {        url = new URL(docUrl);//docUrl为pdf地址        String path = Environment.getExternalStorageDirectory()                + "/Download/";        String[] splitStrings = docUrl.split("/");        String[] nameStrings = splitStrings[splitStrings.length - 1]                .split(".pdf");        String fileName = nameStrings[0] + ".pdf";//当pdf的url带签名时,文件名过长下载会失败        File file = new File(path + fileName);        ExecutorService executorService = Executors                .newSingleThreadExecutor();        Downloader downloader = new Downloader();        downloader.setUrlAndFile(url, file);        File downloadFile = executorService.submit(downloader)                .get();        // loadingDialog.dismiss();        Uri path1 = Uri.fromFile(downloadFile);        Intent intent = new Intent(Intent.ACTION_VIEW);        intent.addCategory(Intent.CATEGORY_DEFAULT);        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        intent.setDataAndType(path1, "application/pdf");        try {            this.startActivity(Intent.createChooser(intent,""));        } catch (ActivityNotFoundException e) {            ToastUtil.show(this, "打开失败");        }    } catch (MalformedURLException e) {        e.printStackTrace();    } catch (InterruptedException e) {        e.printStackTrace();    } catch (ExecutionException e) {        e.printStackTrace();    }}

2.关于7.0以上的调用 有2种方法 

第一种方法百度有很多,比较麻烦,大家都能查到

第二种方法: Intent.createChooser()这个方法是启动应用选择器

Intent intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

更多相关文章

  1. 解决URl中文路径乱码问题
  2. Android(安卓)Studio编译输出apk文件修改文件名
  3. Android对不同DPI的dimen选择优先级问题
  4. Android(安卓)Studio中导入依赖库的方法
  5. Android反编译工具的使用-Android(安卓)Killer
  6. Android(安卓)Fragmnet的使用新体会
  7. android fragment 切换时隐藏软键盘
  8. Android中的文件存储数据方式 .
  9. Android(安卓)TextView文字滚动

随机推荐

  1. 百分点认知智能实验室:基于不完全标注样本
  2. 软件开发之交付清单详解
  3. kubernetes ingress 原理、部署
  4. 一个Python程序,可以帮助您自动备份文件
  5. 系统之眼!Linux系统性能监控工具Glances
  6. 介绍Dynamics 365/Power Apps生成PDF的功
  7. MuleSoft网站的架构变迁
  8. 必备技能!Shell编程实战案例分享(PDF版)!
  9. 5s!用浏览器打造一个开箱即用的Linux系统
  10. 良心国产工具,比Xshell好用还免费!