android - 打开具有特定相册/文件夹的默认照片应用程序

标签 android android-intent android-gallery android-external-storage android-fileprovider

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

我希望启动一个 Intent,该 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>

主 Activity onCreate() 中的 Intent 启动代码:

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.

除了“没有可靠的方法可以做到这一点”以外的任何答案都是错误的。

Since Android 7.0, the following method does not work

它在 Android 7.0 之前也不起作用。如果 path 指向一个目录,那么您就是在向第三方应用撒谎,说该位置有图像,但实际上那里没有图像(因为它是一个目录,而不是一个目录)文件)。我预计大多数 ACTION_VIEW 应用都无法很好地处理。期望它们神奇地显示目录内容只是一厢情愿——支持 ACTION_VIEW 的应用程序并不需要图像具有显示目录内容的能力,更不用说掉落了在收到损坏的 ACTION_VIEW Intent 后返回到该行为。

I am trying using a FileProvider

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

此外,由于 ACTION_VIEW 不支持目录,因此您将回到开始的地方。

(另请注意,您的 grantUriPermission() 调用不会执行任何有意义的操作)

关于android - 打开具有特定相册/文件夹的默认照片应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58048981/

相关文章:

android - 即使应用程序卸载后,WorkManager 实例仍保持 Activity 状态(?)

android - 从 Java 到 Kotlin : synchronization and lock/wait/notify pattern

android - 如何在React-native中将图像从图库复制到应用程序的安装目录

java - 将 ImageView 保存到 Gallery - 代码不工作

android - 在我的应用程序中显示来自设备的所有图像

java - 观察数据库中的@Relation 变化

android - 即使照片未被用户接受,相机 Intent onActivityResult 代码也会保存(空白)图像

android - 来自 Intent.ACTION_OPEN_DOCUMENT 提供的 Android URI 的文件上次修改时间

android - 防止画廊在项目选择上滚动

android - 是否可以从其他应用程序中卸载一个 Android 应用程序?