简体   繁体   English

如何在Android 8.0中使用默认库查看图像?

[英]How to view an image with default gallery in Android 8.0?

I wanted to view my recently downloaded image with my default gallery app in my Android 8.0 [Oreo] . 我想在Android 8.0 [Oreo]中使用默认的图库应用程序查看最近下载的图像。

I have been using intent for that, but it shows notification as " Media not found ". 我一直在使用Intent,但它的通知显示为“ 找不到媒体 ”。

After doing bit search on it, I found that, for " Android N and above " we need to use "FileProvider" but I did not found good explanatory post for that, so please help me on it..... 在对它进行位搜索之后,我发现,对于“ Android N及以上版本 ”,我们需要使用“ FileProvider”,但是我没有找到很好的说明性文章,因此请就此进行帮助。

Please help me by either using intent or file provider: 请通过使用Intent或文件提供程序来帮助我:

My code is as follows: 我的代码如下:

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);
}

if your app targets Android N (7.0) and above, you must use a ContentProvider. 如果您的应用定位到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);

manifest: 表现:

<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 : 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM