文件选择意图错误(将结果ResultInfo传递给活动失败)

时间:2014-11-20 10:25:23

标签: android android-intent

我已经编写了一个用于从设备中选择文件的小代码。

代码段

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("application/pdf");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            startActivityForResult(
                    Intent.createChooser(intent, "Select a File to Upload"),
                    FILE_SELECT_CODE);
        } catch (android.content.ActivityNotFoundException ex) {
            // Potentially direct the user to the Market with a Dialog
            Toast.makeText(this, "Please install a File Manager.",
                    Toast.LENGTH_SHORT).show();
        }



 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
            case FILE_SELECT_CODE:
                if (resultCode == RESULT_OK) {
                    // Get the Uri of the selected file
                    Uri uri = data.getData();                   
                    // Get the path        
                try {
                    path = FileUtils.getPath(this, uri);
                    file = new File(path);

                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Toast.makeText(this, path, Toast.LENGTH_SHORT).show();              

                // open and display the pdf
                display(file, true);
            }
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

如果用户选择外部文件资源管理器来选择文件,则代码可以正常工作 但如果用户删除内置选项(如下载),则应用程序崩溃。

enter image description here

错误记录

11-20 12:56:01.727: E/AndroidRuntime(928): FATAL EXCEPTION: main
11-20 12:56:01.727: E/AndroidRuntime(928): Process: com.example.pdftest, PID: 928
11-20 12:56:01.727: E/AndroidRuntime(928): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/1250 flg=0x1 }} to activity {com.example.pdftest/com.example.pdftest.MainActivity}: java.lang.NullPointerException
11-20 12:56:01.727: E/AndroidRuntime(928):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3432)
11-20 12:56:01.727: E/AndroidRuntime(928):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475)
11-20 12:56:01.727: E/AndroidRuntime(928):  at android.app.ActivityThread.access$1300(ActivityThread.java:139)
11-20 12:56:01.727: E/AndroidRuntime(928):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
11-20 12:56:01.727: E/AndroidRuntime(928):  at android.os.Handler.dispatchMessage(Handler.java:102)
11-20 12:56:01.727: E/AndroidRuntime(928):  at android.os.Looper.loop(Looper.java:136)
11-20 12:56:01.727: E/AndroidRuntime(928):  at android.app.ActivityThread.main(ActivityThread.java:5086)
11-20 12:56:01.727: E/AndroidRuntime(928):  at java.lang.reflect.Method.invokeNative(Native Method)
11-20 12:56:01.727: E/AndroidRuntime(928):  at java.lang.reflect.Method.invoke(Method.java:515)
11-20 12:56:01.727: E/AndroidRuntime(928):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-20 12:56:01.727: E/AndroidRuntime(928):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-20 12:56:01.727: E/AndroidRuntime(928):  at dalvik.system.NativeStart.main(Native Method)
11-20 12:56:01.727: E/AndroidRuntime(928): Caused by: java.lang.NullPointerException
11-20 12:56:01.727: E/AndroidRuntime(928):  at java.io.File.fixSlashes(File.java:185)
11-20 12:56:01.727: E/AndroidRuntime(928):  at java.io.File.<init>(File.java:134)
11-20 12:56:01.727: E/AndroidRuntime(928):  at com.example.pdftest.MainActivity.onActivityResult(MainActivity.java:69)
11-20 12:56:01.727: E/AndroidRuntime(928):  at android.app.Activity.dispatchActivityResult(Activity.java:5446)
11-20 12:56:01.727: E/AndroidRuntime(928):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3428)
11-20 12:56:01.727: E/AndroidRuntime(928):  ... 11 more

0 个答案:

没有答案