Android Intents - SetDataAndType与setData和setType之间的区别?

Tay*_*ton 3 android-intent

所以我正在编写一个意图,应该启动画廊来查看通过它的Uri传递到意图中的图像.这段代码工作正常,完全符合我的要求:

private Intent makeGalleryIntent(String pathToImageFile) {
    Intent mGalleryIntent = new Intent(Intent.ACTION_VIEW);
    mGalleryIntent.setDataAndType(Uri.parse("file://" + pathToImageFile), "image/*");
    return mGalleryIntent;
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用它时(只是在我学习的时候尝试不同的东西),这个代码要么在模拟器上崩溃,因为它无法启动相机,或者只是在我的物理设备上拉起我的图库:

private Intent makeGalleryIntent(String pathToImageFile) {
    Intent mGalleryIntent = new Intent(Intent.ACTION_VIEW);
    mGalleryIntent.setData(Uri.parse("file://" + pathToImageFile));
    mGalleryIntent.setType("image/*");
    return mGalleryIntent;
}
Run Code Online (Sandbox Code Playgroud)

他们看起来都应该做同样的事情.另外,有没有一种方法可以使用Intent构造函数正确设置所有这些?

小智 8

从文档:意图文档

要仅设置数据URI,请调用setData().要仅设置MIME类型,请调用setType().如有必要,可以使用setDataAndType()显式设置它们.

警告:如果要同时设置URI和MIME类型,请不要调用setData()和setType(),因为它们都会使另一个的值无效.始终使用setDataAndType()来设置URI和MIME类型.

我从来没有真正得到一个答案,为什么他们会相互抵消..如果你有一个想法,为什么id喜欢听到它!