Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit 8b8fb00

Browse files
committed
Add mechanism for providing pdf documents from different sources
Add shorthand methods for using document sources Deprecate FileNotFoundException and remove all usages Add util method toByteArray Update PdfiumAndroid version
1 parent 878cd30 commit 8b8fb00

File tree

11 files changed

+289
-76
lines changed

11 files changed

+289
-76
lines changed

android-pdf-viewer/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ android {
3838
}
3939

4040
dependencies {
41-
compile 'com.github.barteksc:pdfium-android:1.4.0'
41+
compile 'com.github.barteksc:pdfium-android:1.5.0'
4242
}
4343

4444
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/DecodingAsyncTask.java

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,78 +16,43 @@
1616
package com.github.barteksc.pdfviewer;
1717

1818
import android.content.Context;
19-
import android.net.Uri;
2019
import android.os.AsyncTask;
21-
import android.os.ParcelFileDescriptor;
2220

23-
import com.github.barteksc.pdfviewer.util.FileUtils;
21+
import com.github.barteksc.pdfviewer.source.DocumentSource;
2422
import com.shockwave.pdfium.PdfDocument;
2523
import com.shockwave.pdfium.PdfiumCore;
2624

27-
import java.io.File;
28-
import java.io.IOException;
29-
3025
class DecodingAsyncTask extends AsyncTask<Void, Void, Throwable> {
3126

3227
private boolean cancelled;
3328

34-
private String path;
35-
36-
private boolean isAsset;
37-
3829
private PDFView pdfView;
3930

4031
private Context context;
4132
private PdfiumCore pdfiumCore;
4233
private PdfDocument pdfDocument;
4334
private String password;
35+
private DocumentSource docSource;
4436

45-
public DecodingAsyncTask(String path, boolean isAsset, String password, PDFView pdfView, PdfiumCore pdfiumCore) {
37+
public DecodingAsyncTask(DocumentSource docSource, String password, PDFView pdfView, PdfiumCore pdfiumCore) {
38+
this.docSource = docSource;
4639
this.cancelled = false;
4740
this.pdfView = pdfView;
48-
this.isAsset = isAsset;
4941
this.password = password;
5042
this.pdfiumCore = pdfiumCore;
51-
this.path = path;
5243
context = pdfView.getContext();
5344
}
5445

5546
@Override
5647
protected Throwable doInBackground(Void... params) {
5748
try {
58-
if (isAsset) {
59-
path = FileUtils.fileFromAsset(context, path).getAbsolutePath();
60-
}
61-
pdfDocument = pdfiumCore.newDocument(getSeekableFileDescriptor(path), password);
49+
pdfDocument = docSource.createDocument(context, pdfiumCore, password);
6250
return null;
6351
} catch (Throwable t) {
6452
return t;
6553
}
6654
}
6755

68-
protected ParcelFileDescriptor getSeekableFileDescriptor(String path) throws IOException {
69-
ParcelFileDescriptor pfd;
70-
71-
File pdfCopy = new File(path);
72-
if (pdfCopy.exists()) {
73-
pfd = ParcelFileDescriptor.open(pdfCopy, ParcelFileDescriptor.MODE_READ_ONLY);
74-
return pfd;
75-
}
76-
77-
if (!path.contains("://")) {
78-
path = String.format("file://%s", path);
79-
}
80-
81-
Uri uri = Uri.parse(path);
82-
pfd = context.getContentResolver().openFileDescriptor(uri, "r");
83-
84-
if (pfd == null) {
85-
throw new IOException("Cannot get FileDescriptor for " + path);
86-
}
87-
88-
return pfd;
89-
}
90-
9156
@Override
9257
protected void onPostExecute(Throwable t) {
9358
if (t != null) {

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/PDFView.java

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@
3131
import android.util.Log;
3232
import android.widget.RelativeLayout;
3333

34-
import com.github.barteksc.pdfviewer.exception.FileNotFoundException;
3534
import com.github.barteksc.pdfviewer.listener.OnDrawListener;
3635
import com.github.barteksc.pdfviewer.listener.OnErrorListener;
3736
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
3837
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
3938
import com.github.barteksc.pdfviewer.listener.OnPageScrollListener;
4039
import com.github.barteksc.pdfviewer.model.PagePart;
4140
import com.github.barteksc.pdfviewer.scroll.ScrollHandle;
41+
import com.github.barteksc.pdfviewer.source.AssetSource;
42+
import com.github.barteksc.pdfviewer.source.ByteArraySource;
43+
import com.github.barteksc.pdfviewer.source.DocumentSource;
44+
import com.github.barteksc.pdfviewer.source.FileSource;
45+
import com.github.barteksc.pdfviewer.source.InputStreamSource;
46+
import com.github.barteksc.pdfviewer.source.UriSource;
4247
import com.github.barteksc.pdfviewer.util.ArrayUtils;
4348
import com.github.barteksc.pdfviewer.util.Constants;
4449
import com.github.barteksc.pdfviewer.util.MathUtils;
@@ -66,7 +71,7 @@
6671
* - DocumentPage = A page of the PDF document.
6772
* - UserPage = A page as defined by the user.
6873
* By default, they're the same. But the user can change the pages order
69-
* using {@link #load(String, boolean, String, OnLoadCompleteListener, OnErrorListener, int[])}. In this
74+
* using {@link #load(DocumentSource, String, OnLoadCompleteListener, OnErrorListener, int[])}. In this
7075
* particular case, a userPage of 5 can refer to a documentPage of 17.
7176
*/
7277
public class PDFView extends RelativeLayout {
@@ -279,11 +284,11 @@ public PDFView(Context context, AttributeSet set) {
279284
setWillNotDraw(false);
280285
}
281286

282-
private void load(String path, boolean isAsset, String password, OnLoadCompleteListener listener, OnErrorListener onErrorListener) {
283-
load(path, isAsset, password, listener, onErrorListener, null);
287+
private void load(DocumentSource docSource, String password, OnLoadCompleteListener listener, OnErrorListener onErrorListener) {
288+
load(docSource, password, listener, onErrorListener, null);
284289
}
285290

286-
private void load(String path, boolean isAsset, String password, OnLoadCompleteListener onLoadCompleteListener, OnErrorListener onErrorListener, int[] userPages) {
291+
private void load(DocumentSource docSource, String password, OnLoadCompleteListener onLoadCompleteListener, OnErrorListener onErrorListener, int[] userPages) {
287292

288293
if (!recycled) {
289294
throw new IllegalStateException("Don't call load on a PDF View without recycling it first.");
@@ -301,7 +306,7 @@ private void load(String path, boolean isAsset, String password, OnLoadCompleteL
301306

302307
recycled = false;
303308
// Start decoding document
304-
decodingAsyncTask = new DecodingAsyncTask(path, isAsset, password, this, pdfiumCore);
309+
decodingAsyncTask = new DecodingAsyncTask(docSource, password, this, pdfiumCore);
305310
decodingAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
306311
}
307312

@@ -1108,47 +1113,48 @@ public List<PdfDocument.Bookmark> getTableOfContents() {
11081113
* Use an asset file as the pdf source
11091114
*/
11101115
public Configurator fromAsset(String assetName) {
1111-
InputStream stream = null;
1112-
try {
1113-
stream = getContext().getAssets().open(assetName);
1114-
return new Configurator(assetName, true);
1115-
} catch (IOException e) {
1116-
throw new FileNotFoundException(assetName + " does not exist.", e);
1117-
} finally {
1118-
try {
1119-
if (stream != null) {
1120-
stream.close();
1121-
}
1122-
} catch (IOException e) {
1123-
1124-
}
1125-
}
1116+
return new Configurator(new AssetSource(assetName));
11261117
}
11271118

11281119
/**
11291120
* Use a file as the pdf source
11301121
*/
11311122
public Configurator fromFile(File file) {
1132-
if (!file.exists()) {
1133-
throw new FileNotFoundException(file.getAbsolutePath() + " does not exist.");
1134-
}
1135-
return new Configurator(file.getAbsolutePath(), false);
1123+
return new Configurator(new FileSource(file));
11361124
}
11371125

11381126
/**
1139-
* Use Uri as the pdf source, for use with content provider
1127+
* Use URI as the pdf source, for use with content providers
11401128
*/
11411129
public Configurator fromUri(Uri uri) {
1142-
return new Configurator(uri.toString(), false);
1130+
return new Configurator(new UriSource(uri));
1131+
}
1132+
1133+
/**
1134+
* Use bytearray as the pdf source, documents is not saved
1135+
* @param bytes
1136+
* @return
1137+
*/
1138+
public Configurator fromBytes(byte[] bytes) {
1139+
return new Configurator(new ByteArraySource(bytes));
1140+
}
1141+
1142+
public Configurator fromStream(InputStream stream) {
1143+
return new Configurator(new InputStreamSource(stream));
1144+
}
1145+
1146+
/**
1147+
* Use custom source as pdf source
1148+
*/
1149+
public Configurator fromSource(DocumentSource docSource) {
1150+
return new Configurator(docSource);
11431151
}
11441152

11451153
private enum State {DEFAULT, LOADED, SHOWN, ERROR}
11461154

11471155
public class Configurator {
11481156

1149-
private final String path;
1150-
1151-
private final boolean isAsset;
1157+
private final DocumentSource documentSource;
11521158

11531159
private int[] pageNumbers = null;
11541160

@@ -1176,9 +1182,8 @@ public class Configurator {
11761182

11771183
private ScrollHandle scrollHandle = null;
11781184

1179-
private Configurator(String path, boolean isAsset) {
1180-
this.path = path;
1181-
this.isAsset = isAsset;
1185+
private Configurator(DocumentSource documentSource) {
1186+
this.documentSource = documentSource;
11821187
}
11831188

11841189
public Configurator pages(int... pageNumbers) {
@@ -1259,9 +1264,9 @@ public void load() {
12591264
PDFView.this.setScrollHandle(scrollHandle);
12601265
PDFView.this.dragPinchManager.setSwipeVertical(swipeVertical);
12611266
if (pageNumbers != null) {
1262-
PDFView.this.load(path, isAsset, password, onLoadCompleteListener, onErrorListener, pageNumbers);
1267+
PDFView.this.load(documentSource, password, onLoadCompleteListener, onErrorListener, pageNumbers);
12631268
} else {
1264-
PDFView.this.load(path, isAsset, password, onLoadCompleteListener, onErrorListener);
1269+
PDFView.this.load(documentSource, password, onLoadCompleteListener, onErrorListener);
12651270
}
12661271
}
12671272
}

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/exception/FileNotFoundException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.github.barteksc.pdfviewer.exception;
1717

18+
@Deprecated
1819
public class FileNotFoundException extends RuntimeException {
1920

2021
public FileNotFoundException(String detailMessage) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2016 Bartosz Schiller.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.barteksc.pdfviewer.source;
17+
18+
19+
import android.content.Context;
20+
import android.os.ParcelFileDescriptor;
21+
22+
import com.github.barteksc.pdfviewer.util.FileUtils;
23+
import com.shockwave.pdfium.PdfDocument;
24+
import com.shockwave.pdfium.PdfiumCore;
25+
26+
import java.io.File;
27+
import java.io.IOException;
28+
29+
public class AssetSource implements DocumentSource {
30+
31+
private final String assetName;
32+
33+
public AssetSource(String assetName) {
34+
this.assetName = assetName;
35+
}
36+
37+
@Override
38+
public PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException {
39+
File f = FileUtils.fileFromAsset(context, assetName);
40+
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
41+
return core.newDocument(pfd, password);
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2016 Bartosz Schiller.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.barteksc.pdfviewer.source;
17+
18+
import android.content.Context;
19+
20+
import com.shockwave.pdfium.PdfDocument;
21+
import com.shockwave.pdfium.PdfiumCore;
22+
23+
import java.io.IOException;
24+
25+
public class ByteArraySource implements DocumentSource {
26+
27+
private byte[] data;
28+
29+
public ByteArraySource(byte[] data) {
30+
this.data = data;
31+
}
32+
33+
@Override
34+
public PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException {
35+
return core.newDocument(data, password);
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2016 Bartosz Schiller.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.barteksc.pdfviewer.source;
17+
18+
import android.content.Context;
19+
20+
import com.shockwave.pdfium.PdfDocument;
21+
import com.shockwave.pdfium.PdfiumCore;
22+
23+
import java.io.IOException;
24+
25+
public interface DocumentSource {
26+
PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException;
27+
}

0 commit comments

Comments
 (0)