gpt4 book ai didi

java - FileProvider 在空对象引用上导致 XmlResourceParser 和 ProviderInfo.loadXmlMetaData 崩溃

转载 作者:行者123 更新时间:2023-12-02 09:00:49 25 4
gpt4 key购买 nike

我对 java 和 android studio 很陌生。我尝试在 android 10 API 29 上下载 pdf 文件后打开它。当从 webview 链接单击文件链接时,它可以正常下载,但无法打开,并且应用程序因 logcat 中的这些错误而崩溃。我有一种感觉,我的代码完全搞砸了,因为试图让它工作有点让人不知所措。

这是 logcat 中的错误

Process: com.principalhomerepair.phrAdmin, PID: 20508
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:605)
at androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:417)
at com.principalhomerepair.phrAdmin.MainActivity$6.onDownloadStart(MainActivity.java:358)
at ic.a(PG:548)
at An.handleMessage(PG:25)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

这是我的 MainActivity.java

public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
if (!check_permission(2)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, file_perm);
} else {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

//Get the name of the file being downloaded from the url
String fileName = url.substring(url.lastIndexOf("/") + 1);

request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription(getString(R.string.dl_downloading));
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
assert dm != null;
dm.enqueue(request);
Toast.makeText(getApplicationContext(), getString(R.string.dl_downloading2) + ": " + fileName, Toast.LENGTH_LONG).show();

//If the file link contains a pdf open it after downloading
if(mimeType.contains("application/pdf")){
File pdfFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + fileName);;

MimeTypeMap mime = MimeTypeMap.getSingleton();
String fileExt = pdfFile.getName().substring(pdfFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(fileExt);

Intent openPdf_Intent = new Intent();
openPdf_Intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
openPdf_Intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "com.your.package.fileProvider", pdfFile);
openPdf_Intent.setDataAndType(contentUri, type);
} else {
openPdf_Intent.setDataAndType(Uri.fromFile(pdfFile), type);
}

startActivity(openPdf_Intent);

Toast.makeText(getApplicationContext(), "Viewing PDF File: " + fileName, Toast.LENGTH_SHORT).show();
}
}
}

文件路径.xml

<?xml version ="1.0" encoding ="utf-8"?>
<paths>
<external-path name="Download" path="Download"/>
</paths>

AnodroidManifest.xml

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

更新了 MainActivity.java

        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {


if (!check_permission(2)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, file_perm);
} else {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

//Get the name of the file being downloaded from the url
String fileName = url.substring(url.lastIndexOf("/") + 1);

request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription(getString(R.string.dl_downloading));
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
assert dm != null;
dm.enqueue(request);
Toast.makeText(getApplicationContext(), getString(R.string.dl_downloading2) + ": " + fileName, Toast.LENGTH_LONG).show();

//If the file link contains a pdf open it after downloading
if(mimeType.contains("application/pdf")){

//Grab the file from the internal storage
File pdfFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + fileName);

MimeTypeMap mime = MimeTypeMap.getSingleton();
String fileExt = pdfFile.getName().substring(pdfFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(fileExt);

Intent openPdf_Intent = new Intent("com.adobe.reader");
openPdf_Intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
openPdf_Intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",pdfFile);

openPdf_Intent.setDataAndType(contentUri, type);
} else {
openPdf_Intent.setDataAndType(Uri.fromFile(pdfFile), type);
}
//startActivityForResult(intent, ACTIVITY_VIEW_ATTACHMENT);
startActivity(openPdf_Intent);

Toast.makeText(getApplicationContext(), "Viewing PDF File: " + fileName, Toast.LENGTH_SHORT).show();
}
}

}
});

应用程序不再崩溃,但不会在 pdf 查看器中打开文件。 Android 设备显示转至 pdf 查看器一瞬间,然后返回有问题的应用程序,仅此而已。它不显示文件

最佳答案

这一行看起来很可疑:

Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "com.your.package.fileProvider", pdfFile);

可能应该是:

BuildConfig.APPLICATION_ID

关于java - FileProvider 在空对象引用上导致 XmlResourceParser 和 ProviderInfo.loadXmlMetaData 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60178295/

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