从 Android 上的新 Google 相册应用中获取照片和视频

vki*_*ins 5 android android-intent google-photos

我希望用户将一堆视频/照片导入我的应用程序。这是我之前使用的代码:

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setType("image/*,video/*");
        activity.startActivityForResult(intent, REQUEST_CODE_PICK_MEDIA);
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,上面只返回来自新 Google 相册应用的照片。如果我仅将数据类型更改为“video/*”,则照片应用会返回视频。这是给 KitKat+ 的

编辑:

我尝试了以下代码 - 它适用于某些画廊,但不适用于大多数画廊,而不是 Google 相册:

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    if (AndroidHelper.isKitKatAndAbove()) {
        Log.d(TAG, "Pick from gallery (KitKat+)");
        String[] mimeTypes = {"image/*", "video/*"};
        intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        activity.startActivityForResult(intent, REQUEST_CODE_PICK_MEDIA);
    } else {
        Log.d(TAG, "Pick from gallery (Compatibility)");
        activity.startActivityForResult(intent, REQUEST_CODE_PICK_MEDIA);
    }
Run Code Online (Sandbox Code Playgroud)

vki*_*ins 1

这就是我最终所做的:

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        if (AndroidHelper.isKitKatAndAbove()) {
            Log.d(TAG, "Pick from gallery (KitKat+)");
            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            activity.startActivityForResult(intent, REQUEST_CODE_PICK_MEDIA);
        } else {
            Log.d(TAG, "Pick from gallery (Compatibility)");
            activity.startActivityForResult(intent, REQUEST_CODE_PICK_MEDIA);
        }        
Run Code Online (Sandbox Code Playgroud)

然后当我得到结果时,我检查文件的类型。似乎工作正常。

  • 在这里写下这一点:https://issuetracker.google.com/issues/123811931。请考虑为其加注星标。现在,我还编写了一种更好的选择文件的方法:/sf/answers/3815178881/ (2认同)