牛骨文教育服务平台(让学习变的简单)
博文笔记

android 打开pdf文件 从inputStream读取数据并保存为文件

创建时间:2015-07-20 投稿人: 浏览次数:428
/**
 * 打开pdf 文件,Get PDF file Intent。
 * @param path 文件路径
 * @return
 */
public static Intent getPdfFileIntent(String path) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.addCategory(Intent.CATEGORY_DEFAULT);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Uri uri = Uri.fromFile(new File(path));
    i.setDataAndType(uri, "application/pdf");
    return i;
}

/**
 * 从inputStream 读取数据 并保存为文件。
 */
public static void inputStreamToFile(InputStream ins, File file) throws IOException {
    OutputStream os = new FileOutputStream(file);
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
        os.write(buffer, 0, bytesRead);
    }
    os.close();
    ins.close();
}

 

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。