gpt4 book ai didi

android - PdfRenderer 无法访问受密码保护的 pdf

转载 作者:行者123 更新时间:2023-12-04 03:03:27 31 4
gpt4 key购买 nike

    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: cannot create document. Error: 410-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.graphics.pdf.PdfRenderer.nativeCreate(Native Method)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.graphics.pdf.PdfRenderer.(PdfRenderer.java:157)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at com.sbw.pdfrendererview.HomeActivity.openRenderer(HomeActivity.java:205)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at com.sbw.pdfrendererview.HomeActivity.onActivityResult(HomeActivity.java:157)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:7273)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4520)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4567)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.ActivityThread.-wrap22(ActivityThread.java)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1695)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.os.Looper.loop(Looper.java:154)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6780)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at java.lang.reflect.Method.invoke(Native Method)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

最佳答案

PdfRenderer 并非设计用于呈现任意 PDF 文件。它专为在应用程序内创建的用于打印的 PDF 文档的打印预览而设计。因此,PdfRenderer 不支持受密码保护的 PDF 文件。

关于android - PdfRenderer 无法访问受密码保护的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46811204/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com