下载一个PDF文件并将其保存到SD卡,然后从那里读 [英] Download a pdf file and save it to sdcard and then read it from there

查看:154
本文介绍了下载一个PDF文件并将其保存到SD卡,然后从那里读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  从URL 下载PDF阅读它

Possible Duplicate:
Download PDF from url and read it

我必须从网址下载一个PDF文件并将其保存到SD卡上,然后去读它。 我经历了很多codeS了,我发现这个

I have to download a pdf file from an url and save it to the sd card and then to read it. I got through many codes and i found this

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("*url for your pdf*"));
startActivity(browserIntent);

但如何将它保存在SD卡在我所希望的路径,然后从那里阅读。

but how to save it in sd-card in my desired path and then read it from there.

推荐答案

请看一看这个的链接

它包含了你的需求的例子。下面还有在链接的信息的摘要。

It Contains an example of your requirement. Below there is a summary of the information in the link.

第一步在AndroidManifest.xml中声明persmissions

First step declaring persmissions in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

创建一个下载器类

Create a downloader class

public class Downloader {

    public static void DownloadFile(String fileURL, File directory) {
        try {

            FileOutputStream f = new FileOutputStream(directory);
            URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            InputStream in = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

最后,创建从互联网上下载的PDF文件中的一个活动,

Finally creating an activity which downloads the PDF file from internet,

public class PDFFromServerActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String extStorageDirectory = Environment.getExternalStorageDirectory()
        .toString();
        File folder = new File(extStorageDirectory, "pdf");
        folder.mkdir();
        File file = new File(folder, "Read.pdf");
        try {
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Downloader.DownloadFile("http://www.nmu.ac.in/ejournals/aspx/courselist.pdf", file);

        showPdf();
    }
    public void showPdf()
        {
            File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
            PackageManager packageManager = getPackageManager();
            Intent testIntent = new Intent(Intent.ACTION_VIEW);
            testIntent.setType("application/pdf");
            List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/pdf");
            startActivity(intent);
        }
}

这篇关于下载一个PDF文件并将其保存到SD卡,然后从那里读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆