aboutsummaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
authorYuichi Araki <yaraki@google.com>2016-12-20 02:16:29 +0000
committerandroid-build-merger <android-build-merger@google.com>2016-12-20 02:16:29 +0000
commit76e8494532e44bf15263855ecb9197ab89a8397a (patch)
tree74fa193094928033c75ccb5ddb928f9b419bc226 /ui
parentc276513065f1f73095d80efa7b23e65161b6df15 (diff)
parentecd1c870e29f8e34b25fd014dae1e088c8589010 (diff)
downloadandroid-76e8494532e44bf15263855ecb9197ab89a8397a.tar.gz
Work around an issue with aapt am: 7b6e65db15 am: 290c6f1614 am: 54d84a1a0e
am: ecd1c870e2 Change-Id: Ibc67989560a99e5095ca93a6302866a9d691daa3
Diffstat (limited to 'ui')
-rw-r--r--ui/graphics/PdfRendererBasic/Application/src/main/java/com/example/android/pdfrendererbasic/PdfRendererBasicFragment.java24
-rw-r--r--ui/graphics/PdfRendererBasic/template-params.xml15
2 files changed, 25 insertions, 14 deletions
diff --git a/ui/graphics/PdfRendererBasic/Application/src/main/java/com/example/android/pdfrendererbasic/PdfRendererBasicFragment.java b/ui/graphics/PdfRendererBasic/Application/src/main/java/com/example/android/pdfrendererbasic/PdfRendererBasicFragment.java
index f867f47d..98f16941 100644
--- a/ui/graphics/PdfRendererBasic/Application/src/main/java/com/example/android/pdfrendererbasic/PdfRendererBasicFragment.java
+++ b/ui/graphics/PdfRendererBasic/Application/src/main/java/com/example/android/pdfrendererbasic/PdfRendererBasicFragment.java
@@ -29,7 +29,10 @@ import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
/**
* This fragment has a big {@ImageView} that shows PDF pages, and 2
@@ -45,6 +48,11 @@ public class PdfRendererBasicFragment extends Fragment implements View.OnClickLi
private static final String STATE_CURRENT_PAGE_INDEX = "current_page_index";
/**
+ * The filename of the PDF.
+ */
+ private static final String FILENAME = "sample.pdf";
+
+ /**
* File descriptor of the PDF.
*/
private ParcelFileDescriptor mFileDescriptor;
@@ -141,7 +149,21 @@ public class PdfRendererBasicFragment extends Fragment implements View.OnClickLi
*/
private void openRenderer(Context context) throws IOException {
// In this sample, we read a PDF from the assets directory.
- mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor();
+ File file = new File(context.getCacheDir(), FILENAME);
+ if (!file.exists()) {
+ // Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
+ // the cache directory.
+ InputStream asset = context.getAssets().open(FILENAME);
+ FileOutputStream output = new FileOutputStream(file);
+ final byte[] buffer = new byte[1024];
+ int size;
+ while ((size = asset.read(buffer)) != -1) {
+ output.write(buffer, 0, size);
+ }
+ asset.close();
+ output.close();
+ }
+ mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
// This is the PdfRenderer we use to render the PDF.
if (mFileDescriptor != null) {
mPdfRenderer = new PdfRenderer(mFileDescriptor);
diff --git a/ui/graphics/PdfRendererBasic/template-params.xml b/ui/graphics/PdfRendererBasic/template-params.xml
index c7182ebb..81ea8268 100644
--- a/ui/graphics/PdfRendererBasic/template-params.xml
+++ b/ui/graphics/PdfRendererBasic/template-params.xml
@@ -33,10 +33,6 @@
</intro>
</strings>
- <aapt>
- <noCompress>pdf</noCompress>
- </aapt>
-
<template src="base"/>
<metadata>
@@ -71,15 +67,8 @@ rendering with [openPage()][3], then call [render()][4] to turn the opened
[PdfRenderer.Page][5] into a bitmap.
This sample loads the PDF from assets. Contents of assets are compressed by
-default, but we disable it since PdfRenderer class cannot handle it.
-
-```groovy
-android {
- aaptOptions {
- noCompress "pdf"
- }
-}
-```
+default, and the PdfRenderer class cannot open it. In this sample, we work
+around this by copying the file into the cache directory.
[1]: https://developer.android.com/reference/android/graphics/pdf/PdfRenderer.html
[2]: https://developer.android.com/reference/android/os/ParcelFileDescriptor.html