public void onBrowse(View view) {
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
chooseFile.setType("file/*");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) return;
String path = "";
if (requestCode == ACTIVITY_CHOOSE_FILE) {
Uri uri = data.getData();
Log.v("Path:", uri.getPath());
try {
openRenderer(HomeActivity.this, uri.getPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void openRenderer(Context context, String path) throws IOException {
// In this sample, we read a PDF from the assets directory.
File file = new File(path);
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);
if (mFileDescriptor != null) {
try {
mPdfRenderer = new PdfRenderer(mFileDescriptor);
} catch (SecurityException e) {
Log.v("password", e.getMessage());
e.printStackTrace();
}
}
}
问题是受密码保护的 pdf 正在获得安全异常。
java.lang.SecurityException:无法创建文档。错误:4 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:在 android.graphics.pdf.PdfRenderer.nativeCreate(本机方法) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 android.graphics.pdf.PdfRenderer.(PdfRenderer.java:157) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 com.sbw.pdfrendererview.HomeActivity.openRenderer(HomeActivity.java:205) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 com.sbw.pdfrendererview.HomeActivity.onActivityResult(HomeActivity.java:157) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 android.app.Activity.dispatchActivityResult(Activity.java:7273) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 android.app.ActivityThread.deliverResults(ActivityThread.java:4520) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:4567) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 android.app.ActivityThread.-wrap22(ActivityThread.java) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1695) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 android.os.Handler.dispatchMessage(Handler.java:102) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 android.os.Looper.loop(Looper.java:154) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 android.app.ActivityThread.main(ActivityThread.java:6780) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:在 java.lang.reflect.Method.invoke(本机方法) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err: 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)