java.lang.RuntimeException: сбой при доставке результата ResultInfo {who = null, request = 4, result = -1, data = Intent - PullRequest
0 голосов
/ 13 мая 2019

это моя ошибка

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 напрямую ошибка

1 Ответ

1 голос
/ 13 мая 2019

посмотрите на функцию ниже

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

filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
    name="external_files"
    path="." />
</paths>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...