【问题标题】:Download a pdf file and save it to sdcard and then read it from there [duplicate]下载pdf文件并将其保存到sdcard,然后从那里读取[重复]
【发布时间】:2012-03-18 14:40:33
【问题描述】:

可能重复:
Download PDF from url and read it

我必须从一个 url 下载一个 pdf 文件并将其保存到 sd 卡中,然后才能阅读它。 我通过了很多代码,我发现了这个

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

但是如何将它保存在我想要的路径中的 sd 卡中,然后从那里读取它。

【问题讨论】:

    标签: android


    【解决方案1】:

    请看一下这个link

    它包含您要求的示例。下面是链接中的信息摘要。

    第一步在 AndroidManifest.xml 中声明权限

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

    创建一个下载器类

    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 文件的活动,

    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);
            }
    }
    

    【讨论】:

    • 如果文件下载失败或下载文件时用户杀死了应用程序,那么这段代码就没有用了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 2013-12-13
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多