Android共享失败,请在Telegram的WhatsApp中重试



这是我要共享的图像的URI

/storage/仿真/0/cr7/imageapr 21,2018年6:40:00 pm.jpeg

在单击"重定向到我选择WhatsApp,Telegram"中的包裹中

rl_share.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(checkPermissions()) {
            Uri uri5 = Uri.parse(myuri);
            Intent t = new Intent(Intent.ACTION_SEND);
            t.setType("image/*");
            t.putExtra(Intent.EXTRA_STREAM, uri5);
            startActivity(Intent.createChooser(t, "Share Image"));
        }
    }
});

您无法通过在Android 8.0及更高版本上使用Uri uri5 = Uri.parse(myuri);获得URI,您必须在应用程序中使用文件提供商来获取uri作为文件,也需要将Intent.FLAG_GRANT_READ_URI_PERMISSION添加到您的共享意图,然后只有第三个应用程序才能访问文件

样品res-> xml-> provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

创建虚拟类,该类在我们的情况下扩展了fileprocider.java

import android.support.v4.content.FileProvider;
public class GenericFileProvider extends FileProvider {
}

在您的清单中。xml在应用程序标签中添加这些行

<provider
            android:name=".GenericFileProvider"
            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>

现在,您可以使用应用程序中的Bellow代码获取uri

Uri screenshotUri = FileProvider.getUriForFile(SelectedImageActivity1.this, getApplicationContext().getPackageName() + ".provider", file);

最新更新