Java “启动意图”;默默地;

Java “启动意图”;默默地;,java,android,android-intent,Java,Android,Android Intent,接下来的问题是,有没有一种方法可以在安卓系统中启动意图而不提示用户做任何事情 现在,我正在检索图像,如下所示: public void changeImage(View view) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult( Intent.

接下来的问题是,有没有一种方法可以在安卓系统中启动意图而不提示用户做任何事情

现在,我正在检索图像,如下所示:

public void changeImage(View view) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(
        Intent.createChooser(intent, getResources().getString(R.string.select_picture)),
        PICK_IMAGE);
}
然后,我存储Uri,并在必要时显示图像(实际上,我首先调整它的大小,但这并不重要):

我希望通过“静默地”调用intent来检索给定Uri的图像数据,以便获得相关的权限。所以我需要像这样的东西:

编辑:

我尝试了
setPackage()
方法。此代码具有以下行为:

  • 如果使用了动作视图意图,库将打开并显示特定图像

  • 如果使用了ACTION_GET_CONTENT intent,则会提示我从库中选择图像,即使我提供了特定的Uri

>


有什么想法吗?

与其尝试发送“静默”意图,一个可能的解决方案是使用“”保存文件的URI——您可以将用户和URI保存为键/值对(例如)


当您想稍后打开文件时,可以在不发送意图的情况下打开该文件。

您应该能够通过以下方式获得“真实”uri(不会更改的uri):

这是针对音频文件的,所以请使用“MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI”并更改其他细节以匹配您的案例

成衣前kat:

    String songName = "";
    Cursor cursor = getContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Audio.Media.TITLE}, MediaStore.MediaColumns.DATA + " =?", new String[]{uri.toString()}, null);

    if (cursor != null && cursor.moveToNext()) {
        songName = cursor.getString(0);
    }

    if (cursor != null) {
        cursor.close();
    }
    return songName;
Kitkat之后,由于文档提供程序的原因发生了更改,此代码应执行以下操作:

Cursor cursor = null;
    String songName = "";
    try {

        String[] column = new String[]{MediaStore.Audio.Media.TITLE};
        String wholeID = DocumentsContract.getDocumentId(uri);
        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];
        // where id is equal to
        String sel = MediaStore.Images.Media._ID + " =?";
        cursor = getContext().getContentResolver().
                query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null);

        if (cursor != null && cursor.moveToNext()) {
            songName = cursor.getString(0);
        }
    }
    catch (Exception e){
        Logger.log(e);
    }
    finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return songName;

同样,更改细节以匹配您的案例,并保存从光标获取的真实uri(可能在投影中获取此字段:MediaStore.Images.Media.DATA)。

您有不同类型的意图,无声的是广播和服务意图。我所说的静默是指默认情况下用户看不到任何活动(没有启动任何活动)

意图只会问系统:有什么东西能够处理它吗? 如果有,它将把它传递给那个东西(如果有多个选项,它将询问用户)。在那之后你没有控制权,除非是你的代码在处理它

你的问题其实是皮卡萨

当用户从Picasa中选择图像时,您会得到一个Uri。您可以将该Uri与ContentResolver一起使用以获取图像。 实际上,Picasa的ContentProvider将处理Uri并返回图像的InputStream

从另一个问题中,我了解到您不会立即获取图像,而是将Uri保存在数据库中,然后再对其进行处理

由于Picasa使用权限机制向您提供图像,并且当您尝试使用Uri时,该权限已过期,因此您希望再次从Picasa获取Uri(以及权限),而无需再次询问用户

问题是Picasa控制着该权限,因此如果Picasa不向您提供通过了解Uri来获得该权限的意图,那么您将无能为力

在我看来,这甚至不是一个bug:当用户决定访问该图像时,您拥有访问该图像的权限,但不会自动从Picasa获得访问每个图像的权限。一个“静默”的服务通过知道每个Picasa图像的Uri为您提供访问每个Picasa图像的权限,只会让您在用户不知情的情况下从用户那里获取任何图像。这可能不是Picasa开发人员想要的


我能给你的最好建议与你在另一个问题中得到的建议相同:当用户选择图像时,立即获取图像并在本地保存,然后使用本地Uri访问图像。

如果
createChooser()
将出现选择对话框。如果您知道将从提供图像的软件包返回图像的意图,则可以直接提高该软件包的意图以提供设置
intent.setPackage
。所以选择器不会出现,接收到意图的软件包会处理它。我可以使用
setData()
来指定Uri吗?以前在Facebook上,当我们打开视频时,有许多意图选项可供您打开视频的软件使用……现在在Facebook上已经有几天了。。当我们点击视频时,它会自动显示视频,而不显示任何视频@parakmiakos您想要相同的吗?这取决于接收包如何使用Intent接口。。。如果你有一个关于如何直接调用的文档,最好遵循that@Softcoder我不确定这是否相同。我不想让应用程序记住在意图中使用的是哪个应用程序。我想在用户不知道的情况下,激发检索特定Uri数据的意图。我需要将此作为我链接的问题中的bug的解决方法。我必须“刷新”Picasa提供的特定Uri的权限。嘿,Francisco。这就是我正在做的。但是,Picasa应用程序提供的URI是一次性的,这意味着我第二次尝试访问相同的URI时,会出现安全异常。然后,您可以尝试在第一次读取图像时创建图像的副本。然后你可以从图像的副本中访问。这是链接问题中提出的解决方案,也是我正在做的工作。不过,我希望能找到一种方法来实现这一目标。谢谢你的回答。我觉得奇怪的是,在不提示用户输入的情况下,我可以让Picasa使用存储的Uri向我显示特定的图像。这就是我使用
Intent.ACTION\u视图时发生的情况。我希望有某种方法可以利用这一点。如果我不得不猜测他们在应用程序中使用Uri来显示图像。你只是碰巧碰上了。
    String songName = "";
    Cursor cursor = getContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Audio.Media.TITLE}, MediaStore.MediaColumns.DATA + " =?", new String[]{uri.toString()}, null);

    if (cursor != null && cursor.moveToNext()) {
        songName = cursor.getString(0);
    }

    if (cursor != null) {
        cursor.close();
    }
    return songName;
Cursor cursor = null;
    String songName = "";
    try {

        String[] column = new String[]{MediaStore.Audio.Media.TITLE};
        String wholeID = DocumentsContract.getDocumentId(uri);
        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];
        // where id is equal to
        String sel = MediaStore.Images.Media._ID + " =?";
        cursor = getContext().getContentResolver().
                query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null);

        if (cursor != null && cursor.moveToNext()) {
            songName = cursor.getString(0);
        }
    }
    catch (Exception e){
        Logger.log(e);
    }
    finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return songName;