首页 > 解决方案 > 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时出错

标签: androidpdf

解决方案


看下面的功能

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>

推荐阅读