Android的意图 - SetDataAndType,只是使用setData和的setType之间的区别? [英] Android Intents - Difference between SetDataAndType and just setData and setType?

查看:1989
本文介绍了Android的意图 - SetDataAndType,只是使用setData和的setType之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写应启动画廊查看传入的意图与它的URI图像的意图。这code正常工作,不正是我想要它做的:

So I am writing an intent that should launch the gallery to view an image passed into the intent with it's Uri. This code works fine, does exactly what I want it to do:

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

但是,当我试图与它(只是尝试不同的东西出来,因为我学)的发挥,这code无论是在模拟器上崩溃,因为它不能启动相机,或者只是拉起我的画廊在我的身体设备:

But when I tried to play with it (just trying different things out as I learn), this code either crashes on the emulator because it can't launch the camera, or just pulls up my gallery on my physical device:

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

他们都看起来像他们应该做同样的事情。此外,有没有我可以正确地只使用意向构造方法设置所有这一切的方式?

They both look like they should do the exact same thing. Also, is there a way I could have set all of that properly just using the Intent constructor?

推荐答案

从文档:的意向文档

要设置只有数据的URI,调用使用setData()。要设置只MIME类型,调用的setType()。如果有必要,你可以用setDataAndType()同时设置作了明确规定。

To set only the data URI, call setData(). To set only the MIME type, call setType(). If necessary, you can set both explicitly with setDataAndType().

注意:如果要同时设置URI和MIME类型,不要调用使用setData()和的setType(),因为它们都废了对方的价值。始终使用setDataAndType()同时设置URI和MIME类型。

Caution: If you want to set both the URI and MIME type, do not call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.

我从来没有真正得到一个答案,为什么other..If你有一个想法,他们将抵消每个至于为什么ID喜欢听!

Ive never really gotten an answer as to WHY they would nullify each other..If you have an idea as to why id love to hear it!

这篇关于Android的意图 - SetDataAndType,只是使用setData和的setType之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆