文件选择器在Android 4.0.3中无效

时间:2016-12-07 13:50:14

标签: android android-intent filechooser

我正在尝试实现一个简单的Intent.createChooser,用户选择要加载的特定文件,因此我将收到所选文件的URI。

现在这适用于Android 5.1.1和UP。但是在Android 4.0.3(模拟器)上我收到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main 
 android.app.SuperNotCalledException: Activity {android/com.android.internal.app.ChooserActivity} did not call through to super.onStop()
  at android.app.Activity.performStop(Activity.java:4606)
  at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3071)
  at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3130)
  at android.app.ActivityThread.access$1200(ActivityThread.java:123)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1180)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4424)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  at dalvik.system.NativeStart.main(Native Method)

以下是代码:

private void showFileChooser() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            builder.setTitle(R.string.Main_Need_Write_External_Storage_Permission_Title);
            builder.setMessage(R.string.Main_Need_Write_External_Storage_Permission_Message);
            builder.setPositiveButton(R.string.General_Notification_Button_Ok, null);

            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    requestPermissions(new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
                }
            });

            builder.show();
        } else {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");
            intent.addCategory(Intent.CATEGORY_OPENABLE);

            try {
                startActivityForResult(Intent.createChooser(intent, getText(R.string.Main_Select_Recipe_File)), FILE_SELECT_CODE);
            } catch (ActivityNotFoundException ex) {
                Snackbar.make(view, R.string.Main_Install_File_Manager, Snackbar.LENGTH_LONG).show();
            }
        }
    } else {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        try {
            startActivityForResult(Intent.createChooser(intent, getText(R.string.Main_Select_Recipe_File)), FILE_SELECT_CODE);
        } catch (ActivityNotFoundException ex) {
            Snackbar.make(view, R.string.Main_Install_File_Manager, Snackbar.LENGTH_LONG).show();
        }
    }
}

这是OnActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                String filePath = getPath(getActivity(), uri);
                String [] extension = filePath.split("\\.(?=[^\\.]+$)");

                if (extension[1].equals("xsud")) {
                    new parseXML().execute(filePath);
                } else {
                    Snackbar.make(view, R.string.Main_Not_Valid_Extension, Snackbar.LENGTH_SHORT).show();
                }
            }

            break;
    }

    super.onActivityResult(requestCode, resultCode, data);
}

我应该如何在com.android.internal.app.ChooserActivity(内部活动)上调用onStop()?

我希望有人可以帮助我。谢谢!

0 个答案:

没有答案