我有一个上传到firebase存储的PDF的下载URL。现在我想使用url下载pdf,然后使用SharedPreferences
将其存储在应用程序中。像离线保存但在应用程序内,然后在应用程序本身中查看pdf。
我目前拥有的是来自Firebase Storage
和PDF Viewer
的PDF下载链接。
我已经使用了这个依赖Implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
,这是我的代码:
String URL="https://firebasestorage.googleapis.com/v0/b/client40-64886.appspot.com/o/newUpload?alt=media&token=59cec451-ee34-477a-98c6-8314c37c1804";
File file = new File(URL);
pdfView.fromFile(file).load();`
问题是它甚至没有查看pdf。
这个类是一个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;
}
}