注册即可打开自定义类型的文件。假设我有 .cool 文件,如果用户尝试打开它,Android 会询问他们是否愿意使用我的应用程序打开它。怎么办?
您可以将以下内容添加到必须打开文件(在我们的例子中为 pdf)的 Activity 内的 AndroidManifest.xml 文件中
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/pdf" />
</intent-filter>
确保指定正确的 mime 格式。如果用户想打开“pdf”文件,系统将提示她选择您的应用程序。
另请检查 Android 意图过滤器:将应用程序与文件扩展名关联以了解更多详细信息。
我认为你不会选择那个。这是由系统处理的。当您发送要由地图处理的意图时也是如此,例如,如果您有 skyfire,系统会弹出一个窗口,您可以选择应用程序并选择是否希望它始终打开此类文件。
当然,除非您的应用程序是唯一打开此类文件的应用程序。
编辑
我认为如果您希望您的应用程序说“嘿,我能够打开这些 .cool 文件”,您需要使用标签
<intent-filters>
设置 <data>
并指定 mimeType 或 Uri。 查看这里了解更多详情。
这是一个更完整的示例,展示如何注册您的应用程序以打开纯文本文件。
在清单中的活动中添加
intent-filter
。就我而言,将显示文本文件的是 ReaderActivity
。 mimeType
表示我将接受任何纯文本文件。请参阅 this 了解特定文件扩展名。
<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>
从您的活动中的意图获取数据,如下所示:
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();
}
}
您可以使用
getData()
从意图中获取数据,以获取文件的 URI。然后,您使用内容解析器打开输入流。您使用内容解析器是因为否则您可能无法直接访问该文件。感谢这个答案提供了将输入流转换为字符串的方法。
显示了一个可能的答案 这里。在您的活动标签之一中使用它,当按下文件时它将被打开。我使用 Oreo 中的本机 Android 文件管理器对其进行了测试。
<intent-filter android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.OPENABLE" />
<data android:host="*" />
<data android:mimeType="application/octet-stream" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\.yourextension" />
<data android:pathPattern=".*\\.yourextension" />
<data android:scheme="content" />
</intent-filter>