Android intent filter: associate app with file extension

后端 未结 16 1952
北海茫月
北海茫月 2020-11-22 11:06

I have a custom file type/extension that I want to associate my app with.

As far as I know, the data element is made for this purpose, but I can\'t get it working. h

16条回答
  •  春和景丽
    2020-11-22 11:46

    Markus Ressel is correct. Android 7.0 Nougat no longer permits file sharing between apps using a file URI. A content URI must be used. However, a content URI does not allow a file path to be shared, only a mime type. So you cannot use a content URI to associate your app with your own file extension.

    Drobpox has an interesting behavior on Android 7.0. When it meets an unknown file extension it appears to form a file URI intent but instead of launching the intent it calls the operating system to find out which apps can accept the intent. If there is only one app that can accept that file URI it then sends an explicit content URI directly to that app. So to work with Dropbox you do not need to change the intent filters on your app. It does not require a content URI intent filter. Just make sure the app can receive a content URI and your app with your own file extension will work with Dropbox just like it did before Android 7.0.

    Here is an example of my file loading code modified to accept a content URI:

    Uri uri = getIntent().getData();
    if (uri != null) {
        File myFile = null;
        String scheme = uri.getScheme();
        if (scheme.equals("file")) {
            String fileName = uri.getEncodedPath();
            myFile = new File(filename);
        }
        else if (!scheme.equals("content")) {
            //error
            return;
        }
        try {
            InputStream inStream;
            if (myFile != null) inStream = new FileInputStream(myFile);
            else inStream = getContentResolver().openInputStream(uri);
            InputStreamReader rdr = new InputStreamReader(inStream);
            ...
        }
    }
    

提交回复
热议问题