如何使用事件通道在Flutter中获取选定的文件路径?

时间:2018-09-25 06:49:28

标签: android flutter

如何使用EventChannel在抖动中获取选定的文件路径?我已经使用了下面的代码。

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
        new EventChannel(getFlutterView(), CHARGING_CHANNEL).setStreamHandler(
                new StreamHandler() {
                    private BroadcastReceiver chargingStateChangeReceiver;

                    @Override
                    public void onListen(Object arguments, EventSink events) {

                        /* final String action = intent.getAction();*/
 final Intent intent = getIntent();
                        Bundle extras = intent.getExtras();
                        Uri data = intent.getData();
                        Uri audoUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
                        String action = intent.getAction();
                        Log.w("========================1111 ", " intent: " + intent);
                        Log.w("=====================1111 ", " action: " + action);
                        Log.w("========================1111 ", " extras: " + extras);
                        Log.w("=========================1111 ", " data: " + data);
                        Log.w("======================1111 ", " audoUri: " + audoUri);
                        Log.w("=======================================1111 ", " Intent.ACTION_VIEW: " + Intent.ACTION_VIEW);

                        if (Intent.ACTION_VIEW.equals(action)) {
                            final List<String> segments = intent.getData().getPathSegments();
                            Log.w("=======================================1111 ", " segments: " + segments);
                            if (segments.size() > 1) {
                                String mUsername = segments.get(1);
                            }
                        }

                        events.success("charging/discharging inside itself");
                    }

                    @Override
                    public void onCancel(Object arguments) {
                        unregisterReceiver(chargingStateChangeReceiver);
                        chargingStateChangeReceiver = null;
                    }
                }
        );
    }

我尝试了上面的代码,如何通过在flutter中使用EventChannel获取文件路径。但是我得到了下面的 android.intent.action.RUN 。如何获取文件路径。

W/========================1111 (15458):  intent: Intent { act=android.intent.action.RUN flg=0x30000000 cmp=com.now.flutter/.MainActivity (has extras) }
W/===============1111 (15458):  action: android.intent.action.RUN
W/====================1111 (15458):  extras: Bundle[{enable-checked-mode=true, enable-dart-profiling=true, enable-background-compilation=true}]
W/================1111 (15458):  data: null
W/===============1111 (15458):  audoUri: null

和我的 AndroidManifest 文件是

<intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="*/*" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.SEND"/>

            <category android:name="android.intent.category.DEFAULT"/>
            <action android:name="android.intent.action.GET_CONTENT" />
            <data android:mimeType="application/pdf"/>

        </intent-filter>

0 个答案:

没有答案