如何从Firebase存储下载PDF并将其保存为共享首选项并在应用程序中查看?

时间:2019-04-22 09:41:45

标签: android firebase pdf sharedpreferences firebase-storage

我有我上传到Firebase存储的PDF的下载URL。现在,我想使用url下载pdf,然后使用# git branch info if present parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' } export PS1="\[\033[36m\]\u@\h\[\033[00m\] \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] \n\[\033[1;31m\]>>\[\033[00m\] " 将其存储在应用程序中。类似于离线保存,但可以在应用程序内进行,然后在应用程序本身中查看pdf。

我目前拥有的是应用程序中添加的SharedPreferencesFirebase Storage的PDF下载链接。

我已经使用了这种依赖性PDF Viewer,这是我的代码:

Implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

问题是它甚至没有查看pdf。

1 个答案:

答案 0 :(得分:0)

此类是pdf下载的帮助程序,但您应实现gradle ion库以使用此类。 enter link description here 另外,创建设备的文件目录。因为这不是保存共享首选项中的pdf文件的最佳方法。 enter link description here

 public class DownloadHelper {
        public static final String TAG = DownloadHelper.class.getSimpleName();
        SettingsHelper settingsHelper;
        Context context;

        ProgressUpdateListener progressUpdateListener;

        private static DownloadHelper instance;

        private HashMap<String, Integer> percents;

        public static DownloadHelper getInstance(Context context) {
            if (instance == null) {
                instance = new DownloadHelper(context);
            }
            return instance;
        }

        public DownloadHelper(Context context) {
            this.context = context;
            percents = new HashMap<>();
            settingsHelper=new SettingsHelper(context);
        }
        public int getDownloadPercentage(String id) {
            if (percents != null && percents.containsKey(id)) {
                return percents.get(id);
            }
            return 0;
        }

        public void setProgressUpdateListener(ProgressUpdateListener progressUpdateListener){
            this.progressUpdateListener = progressUpdateListener;
        }

        public void downloadPdf(final String fileUrl, final String id) {
            //String filePath = Util.getExternalFilesDir(context);
            //String filePath = (Environment.getDataDirectory().getAbsolutePath());
            Ion.getDefault(context).configure().setLogging("ion-sample", Log.DEBUG);
            String filePath = Environment.getExternalStorageDirectory().toString()+"/DownloadPdf";
            File directory = new File(Environment.getExternalStorageDirectory().toString()+"/DownloadPdf");
            directory.mkdir();
            String filename = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
            filePath += "/" + filename;
            String newfileUrl = fileUrl.replace(fileUrl,"http://www.stackoverflow.com/"+fileUrl);

            int downloaded = 0;
            final int start = downloaded;
            try {
                final OutputStream output = new FileOutputStream(filePath);
                Ion.with(context)
                        .load(newfileUrl)
                        .progressHandler(new ProgressCallback() {
                            @Override
                            public void onProgress(final long downloaded, final long total) {
                                int progress = (int) (((downloaded + start) * 100) / (start + total));
                                if (progressUpdateListener != null){
                                    progressUpdateListener.updateProgress(id, progress);
                                }

                                settingsHelper.setProgress(progress);

                            }
                        })
                        .write(output, false)
                        .setCallback(new FutureCallback<OutputStream>() {
                            @Override
                            public void onCompleted(Exception e, OutputStream result) {
                                if (e != null) {
                                    ErrorHandler.handleError(TAG, e);
                                }
                                try {
                                    output.flush();
                                    output.close();


                                }
                                catch (IOException ioE) {
                                    ErrorHandler.handleError("egazetedown",ioE);
                                }
                                Log.d("egazetedown",id+"-result--"+result.toString());
                                if(id!=null&& progressUpdateListener!=null) {
                                    progressUpdateListener.updateStatus(id, true);
                                    settingsHelper.setSavedItemListStatus(id, true);
                                }


                            }
                        });
            }
            catch (Exception e) {
                ErrorHandler.handleError(TAG, e);
            }

        }

        public void stop() {
            instance = null;
        }

        public boolean deleteFile(String filePath) {
            boolean result = false;
            if(filePath!=null) {
                File file = new File(filePath);
                if(file!=null)
                if (file.exists()) {
                    result = file.delete();
                }
            }
            return result;
        }
    }