注册成为自定义文件类型的默认应用程序

Aym*_*ier 17 android file handle android-intent

注册即可打开自定义类型的文件.假设我有.cool文件,如果用户试图打开它,Android会询问他们是否愿意使用我的应用程序打开它.怎么样?

Dan*_*rza 13

您可以将以下内容添加到必须打开文件的活动内的AndroidManifest.xml文件中(在我们的示例中为pdf)

        <intent-filter >
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="application/pdf" />
        </intent-filter>
Run Code Online (Sandbox Code Playgroud)

确保指定正确的mime格式.如果要打开"pdf"文件,系统将提示用户选择您的应用程序.

还可以检查Android意图过滤器:将app与文件扩展名关联以获取更多详细信

  • 实际上,OP说他想打开.cool文件,而不是pdf. (4认同)

Sep*_*phy 11

我想你不要选择那个.这是由系统处理的.发送由地图处理的意图时也是如此,例如,如果你有天空,系统会弹出一个窗口,您可以选择该应用程序并选择是否希望它始终打开这种文件.
当然,除非您的应用程序是唯一一个打开此类文件的应用程序.

编辑
我想如果你想让你的应用说"嘿我能打开这些.cool文件",你需要设置<intent-filters>一个标签<data>并具体说明mimeType或Uri.点击此处了解更多详情.

  • 他在询问如何指定他的应用程序可以处理文件类型。 (2认同)

Sur*_*gch 8

这是一个更完整的示例,展示了如何注册您的应用程序以打开纯文本文件。

注册您可以处理的文件类型

intent-filter在清单中向您的活动添加。在我的情况下ReaderActivity,它将显示文本文件。该mimeType说我将接受任何纯文本文件。有关特定文件扩展名,请参阅此内容

<activity android:name=".ReaderActivity">
    <intent-filter >
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="file"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

处理意图

从您的活动中的意图中获取数据,如下所示:

public class ReaderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reader);

        handleIntent();
    }

    private void handleIntent() {

        Uri uri = getIntent().getData();
        if (uri == null) {
            tellUserThatCouldntOpenFile();
            return;
        }

        String text = null;
        try {
            InputStream inputStream = getContentResolver().openInputStream(uri);
            text = getStringFromInputStream(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (text == null) {
            tellUserThatCouldntOpenFile();
            return;
        }

        TextView textView = findViewById(R.id.tv_content);
        textView.setText(text);
    }

    private void tellUserThatCouldntOpenFile() {
        Toast.makeText(this, getString(R.string.could_not_open_file), Toast.LENGTH_SHORT).show();
    }

    public static String getStringFromInputStream(InputStream stream) throws IOException {
        int n = 0;
        char[] buffer = new char[1024 * 4];
        InputStreamReader reader = new InputStreamReader(stream, "UTF8");
        StringWriter writer = new StringWriter();
        while (-1 != (n = reader.read(buffer))) writer.write(buffer, 0, n);
        return writer.toString();
    }

}
Run Code Online (Sandbox Code Playgroud)

您从意图中获取数据getData()以获取文件的 URI。然后使用内容解析器打开输入流。您使用内容解析器是因为否则您可能无法直接访问该文件。感谢this answer将输入流转换为字符串的方法。