android - java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=4, result=-1, data=Intent
问题描述
这是我的错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.metrasat.msatteknisi, PID: 12101
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/2494 flg=0x1 }} to activity {com.metrasat.msatteknisi/com.metrasat.msatteknisi.Activity.Order.DukcapilForm}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:4179)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4222)
我有一些代码
case AppConst.FILE_BA_PDF:
switch (resultCode) {
case RESULT_OK:
if (requestCode == AppConst.FILE_BA_PDF) {
Uri uri = data.getData();
String uriString = uri.toString();
File myFile;
String path = "";
fileName = "";
if (uriString.startsWith("content://")) {
Cursor cursor = null;
try {
cursor = getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
if (cursor != null &&
cursor.moveToFirst()) {
path = FileUtils.getPath(this, uri);
myFile = new File(path);
fileName = myFile.getName();
}
} finally {
cursor.close();
}
} else if (uriString.startsWith("file://")) {
path = FileUtils.getPath(this, uri);
myFile = new File(path);
fileName = myFile.getName();
} else {
Toast.makeText(this, uriString, Toast.LENGTH_SHORT).show();
}
file_pdf.setText(fileName);
dataMap.put(AppConst.POST_FILE_BA_PDF, path);
}
break;
}
我已经搜索过问题但没有找到,在内部存储中选择pdf时编码此条件,直接选择PDF时出错
解决方案
看下面的功能
private void openPDFFile(String filePath) {
try {
File file = new File(filePath);
if (file.exists()) {
Uri outputFileUri = FileProvider.getUriForFile(context, "com.demo.myapplication.fileprovider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(outputFileUri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(intent);
finish();
} catch (Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("No Application Found");
builder.setMessage("Download one from Android Market?");
builder.setPositiveButton("Yes, Please",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent.setData(Uri.parse("market://details?id=com.adobe.reader"));
startActivity(marketIntent);
finish();
}
});
builder.setNegativeButton("No, Thanks", null);
builder.create().show();
}
} else {
Toast.makeText(context, "File not found!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(context, "Opps something wrong!", Toast.LENGTH_SHORT).show();
}
}
加载项清单文件:-
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.demo.myapplication.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
创建 XML 文件
文件路径.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
推荐阅读
- java - 在java中提取除包含HTML表格的字符串之外的所有字符串数据
- oracle - 命名触发器和序列 - Oracle Data Modeler
- git - 在 FF 合并之前查找最后一次提交
- python-3.x - 如何通过不擦除当前 ACL 来放置 objectacl
- c++ - 如何在 C++ 中为整数和浮点数找到下一个更大的值?
- javascript - 使用 React、Axios 和 Busboy 将图像上传到 Firebase
- c++ - C++ 从变量名加载构造函数
- r - 在R中的行中返回三个最大值的函数
- python - Python xlsxwriter microsoft visual studio 代码导入问题
- jquery - 隐藏一系列 div 中的前 3 个元素