【问题标题】:How to view an image with default gallery in Android 8.0?如何在 Android 8.0 中使用默认图库查看图像?
【发布时间】:2018-11-14 07:10:09
【问题描述】:

我想在 Android 8.0 [Oreo] 中使用我的默认图库应用查看我最近下载的图像。

我一直在为此使用意图,但它显示通知为“找不到媒体”。

查了一下,发现“Android N及以上”需要使用“FileProvider”,但没有找到好的解释贴为此,请帮助我......

请通过使用意图或文件提供程序帮助我:

我的代码如下:

String root = Environment.getExternalStorageDirectory().toString();

public void onClick(DialogInterface dialog, int id)
{
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse(root + "/h_jokes_images/"+fname+".jpg"), "image/mime");
    startActivity(intent);
}

【问题讨论】:

标签: java android-intent android-contentprovider android-gallery android-fileprovider


【解决方案1】:

如果您的应用面向 Android N (7.0) 及更高版本,则必须使用 ContentProvider。

Intent intent = new Intent(Intent.ACTION_VIEW)//
                                    .setDataAndType(VERSION.SDK_INT >= VERSION_CODES.N ?
                                                    android.support.v4.content.FileProvider.getUriForFile(this,getPackageName() + ".provider", file) : Uri.fromFile(file),
                            "image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

清单:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

res/xml/provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!--<external-path name="external_files" path="."/>-->
    <external-path
        name="files_root"
        path="Android/data/${applicationId}"/>
    <external-path
        name="external_storage_root"
        path="."/>
</paths>

【讨论】:

  • 感谢@Sultan Mahmud
猜你喜欢
  • 2013-07-23
  • 2011-07-20
  • 1970-01-01
  • 2022-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多