Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load pdf from url use okhttp3 #366

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,23 @@ This is beta release because of big number of small changes and something could
* removed minimap and mask configuration

## Installation
## Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

## Step 2. Add the dependency
dependencies {
compile 'com.github.shxdos:AndroidPdfViewer:2.7.0-beta.2'
}

Add to _build.gradle_:

`compile 'com.github.barteksc:android-pdf-viewer:2.7.0-beta.1'` or `2.6.1` for more stable version

Library is available in jcenter repository, probably it'll be in Maven Central soon.

## Include PDFView in your layout

Expand All @@ -53,6 +64,16 @@ Library is available in jcenter repository, probably it'll be in Maven Central s
All available options with default values:
``` java
pdfView.fromUri(Uri)
or
pdfView.fromUrl("http://www.anweitong.com/upload/document/standard/national_standards/138793918364316200.pdf")
.enableSwipe(true) // allows to block changing pages using swipe
.defaultPage(0)
.onLoad(this) // called after document is loaded and starts to be rendered
.onPageChange(this)
.swipeHorizontal(false)
.enableAntialiasing(true)
.onFileDownload(this)
.loadFromUrl();
or
pdfView.fromFile(File)
or
Expand Down
1 change: 1 addition & 0 deletions android-pdf-viewer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ android {

dependencies {
compile 'com.github.barteksc:pdfium-android:1.7.0'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
}

apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.HandlerThread;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;

import com.github.barteksc.pdfviewer.listener.OnDrawListener;
import com.github.barteksc.pdfviewer.listener.OnErrorListener;
import com.github.barteksc.pdfviewer.listener.OnFileDownloadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.listener.OnPageScrollListener;
Expand All @@ -47,8 +49,10 @@
import com.github.barteksc.pdfviewer.source.FileSource;
import com.github.barteksc.pdfviewer.source.InputStreamSource;
import com.github.barteksc.pdfviewer.source.UriSource;
import com.github.barteksc.pdfviewer.source.UrlSource;
import com.github.barteksc.pdfviewer.util.ArrayUtils;
import com.github.barteksc.pdfviewer.util.Constants;
import com.github.barteksc.pdfviewer.util.DownloadUtil;
import com.github.barteksc.pdfviewer.util.MathUtils;
import com.github.barteksc.pdfviewer.util.Util;
import com.shockwave.pdfium.PdfDocument;
Expand Down Expand Up @@ -1288,7 +1292,14 @@ public Configurator fromAsset(String assetName) {
public Configurator fromFile(File file) {
return new Configurator(new FileSource(file));
}

/**
* Use a url as the pdf source
*/
public Configurator fromUrl(String url) {
Configurator source= new Configurator(new UrlSource(url));
source.fileUrl(url);
return source;
}
/**
* Use URI as the pdf source, for use with content providers
*/
Expand Down Expand Up @@ -1322,7 +1333,7 @@ private enum State {DEFAULT, LOADED, SHOWN, ERROR}
public class Configurator {

private final DocumentSource documentSource;

private String fileUrl;
private int[] pageNumbers = null;

private boolean enableSwipe = true;
Expand All @@ -1334,6 +1345,7 @@ public class Configurator {
private OnDrawListener onDrawAllListener;

private OnLoadCompleteListener onLoadCompleteListener;
private OnFileDownloadCompleteListener onFileDownloadCompleteListener;

private OnErrorListener onErrorListener;

Expand Down Expand Up @@ -1365,6 +1377,10 @@ public Configurator pages(int... pageNumbers) {
this.pageNumbers = pageNumbers;
return this;
}
private Configurator fileUrl(String fileUrl){
this.fileUrl=fileUrl;
return this;
}

public Configurator enableSwipe(boolean enableSwipe) {
this.enableSwipe = enableSwipe;
Expand Down Expand Up @@ -1395,7 +1411,10 @@ public Configurator onLoad(OnLoadCompleteListener onLoadCompleteListener) {
this.onLoadCompleteListener = onLoadCompleteListener;
return this;
}

public Configurator onFileDownload(OnFileDownloadCompleteListener onFileDownloadCompleteListener) {
this.onFileDownloadCompleteListener = onFileDownloadCompleteListener;
return this;
}
public Configurator onPageScroll(OnPageScrollListener onPageScrollListener) {
this.onPageScrollListener = onPageScrollListener;
return this;
Expand Down Expand Up @@ -1468,5 +1487,40 @@ public void load() {
PDFView.this.load(documentSource, password, onLoadCompleteListener, onErrorListener);
}
}
public void loadFromUrl(){
final String SDPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/PDFViewCache/";
int index = fileUrl.lastIndexOf("/");
String fileName = fileUrl.substring(index);
final File file = new File(SDPath, fileName);
if(file.exists()){
//文件存在
if(onFileDownloadCompleteListener!=null){
onFileDownloadCompleteListener.onDownloadComplete(file);
}
PDFView.this.fromFile(file);
load();
}else{
DownloadUtil.get().download(fileUrl, SDPath, new DownloadUtil.OnDownloadListener() {
@Override
public void onDownloadSuccess(File file) {
if(onFileDownloadCompleteListener!=null){
onFileDownloadCompleteListener.onDownloadComplete(file);
}
PDFView.this.fromFile(file);
load();
}

@Override
public void onDownloading(int progress) {

}

@Override
public void onDownloadFailed() {

}
});
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2016 Bartosz Schiller
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.barteksc.pdfviewer.listener;

import java.io.File;

/**
* Implement this interface to receive events from PDFView
* when the url file loading is complete.
*/
public interface OnFileDownloadCompleteListener {

/**
* Called when the PDF is loaded
* @param file the number of pages in this PDF file
*/
void onDownloadComplete(File file);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.barteksc.pdfviewer.source;

import android.content.Context;
import android.os.Environment;
import android.os.ParcelFileDescriptor;

import com.shockwave.pdfium.PdfDocument;
import com.shockwave.pdfium.PdfiumCore;

import java.io.File;
import java.io.IOException;

/**
* Created by 邵鸿轩 on 2017/7/21.
*/

public class UrlSource implements DocumentSource {
private File file;
private String url;

public UrlSource(String url) {
this.url = url;
}

@Override
public PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException {
final String SDPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFViewCache/";
int index = url.lastIndexOf("/");
String fileName = url.substring(index);
file = new File(SDPath, fileName);
if (file.exists()) {
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
return core.newDocument(pfd, password);
}
return null;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.github.barteksc.pdfviewer.util;

import android.support.annotation.NonNull;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DownloadUtil {

private static DownloadUtil downloadUtil;
private final OkHttpClient okHttpClient;

public static DownloadUtil get() {
if (downloadUtil == null) {
downloadUtil = new DownloadUtil();
}
return downloadUtil;
}

private DownloadUtil() {
okHttpClient = new OkHttpClient();
}

/**
* @param url 下载连接
* @param saveDir 储存下载文件的SDCard目录
* @param listener 下载监听
*/
public void download(final String url, final String saveDir, final OnDownloadListener listener) {
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// 下载失败
listener.onDownloadFailed();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
// 储存下载文件的目录
String savePath = isExistDir(saveDir);
try {
is = response.body().byteStream();
long total = response.body().contentLength();

int index = url.lastIndexOf("/");
String fileName = url.substring(index);
final File file = new File(saveDir, fileName);
fos = new FileOutputStream(file);
long sum = 0;
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
sum += len;
int progress = (int) (sum * 1.0f / total * 100);
// 下载中
listener.onDownloading(progress);
}
fos.flush();
// 下载完成
listener.onDownloadSuccess(file);
} catch (Exception e) {
listener.onDownloadFailed();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
}
}
}
});
}

/**
* @param saveDir
* @return
* @throws IOException
* 判断下载目录是否存在
*/
private String isExistDir(String saveDir) throws IOException {
// 下载位置
File downloadFile = new File( saveDir);
if (!downloadFile.mkdirs()) {
downloadFile.createNewFile();
}
String savePath = downloadFile.getAbsolutePath();
return savePath;
}

/**
* @param url
* @return
* 从下载连接中解析出文件名
*/
@NonNull
private String getNameFromUrl(String url) {
return url.substring(url.lastIndexOf("/") + 1);
}

public interface OnDownloadListener {
/**
* 下载成功
*/
void onDownloadSuccess(File fiel);

/**
* @param progress
* 下载进度
*/
void onDownloading(int progress);

/**
* 下载失败
*/
void onDownloadFailed();
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
Expand Down
Loading