使用特定相册/文件夹打开默认照片应用

Open default Photos app with specific album / folder

不幸的是,该主题的所有现有答案都已完全过时。

我希望启动一个 Intent 以打开带有特定图像文件夹的外部应用程序。 自 Android 7.0 起,以下方法无效:

Intent i=new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(path)), "image/*");  
startActivity(i);

我正在尝试使用 FileProvider。 我的文件提供者:

public class MyFileProvider extends FileProvider {
}

来自 AndroidManifest.xml:

<provider
            android:name=".util.MyFileProvider"
            android:authorities="${applicationId}.common.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/my_file_paths"/>
        </provider>

my_file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="files"
        path="/My_bugreport"/>
    <external-path name="external_files" path="."/>
</paths>

主程序中的 Intent 启动代码 activity onCreate():

Intent i = new Intent();
        i.setAction(Intent.ACTION_VIEW);
        i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath() + "/Pirin 1/"; // Some hard-coded folder I have on my device

        Log.d(TAG, "MyDashboardActivity: onCreate: path=" + path);

        Uri uri = FileProvider.getUriForFile(this, getPackageName() + ".common.fileprovider", new File(path));
        grantUriPermission(getPackageName(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        i.setDataAndType(uri, "image/*");
        startActivity(i);

我得到的照片应用程序有一个空的黑屏和一个旋转的加载程序。 LogCat 不包含任何有用的信息。 设备:Google Pixel 3

已经尝试了很多东西,很多不同的路径等等,几乎要放弃了...

I wish to launch an Intent that will open an external app with a specific images folder.

在 Android 中没有对此的官方支持。

Unfortunately, all existing answers to this topic are totally outdated.

除 "there is no reliable way to do this" 以外的任何答案都是错误的。

Since Android 7.0, the following method does not work

它在 Android 7.0 之前也不起作用。如果path指向一个目录,那么你就是在骗第三方app,说那个位置有图片,其实那里没有图片(因为是目录,不是文件)。我希望大多数 ACTION_VIEW 应用程序处理得不好。期望它们神奇地显示目录的内容只是一厢情愿——没有要求支持 ACTION_VIEW 的应用程序的图像具有显示目录内容的任何能力,更不用说回退到那个了收到损坏的 ACTION_VIEW Intent.

后的行为

I am trying using a FileProvider

FileProvider 不以适合您需要的方式提供目录。例如,FileProvider 无法获取特定目录的内容列表。

此外,由于 ACTION_VIEW 不支持目录,您将回到起点。

(另请注意,您的 grantUriPermission() 调用不会做任何有意义的事情)