Intent.ACTION_SEND 不适用于奥利奥 [重复]

Posted

技术标签:

【中文标题】Intent.ACTION_SEND 不适用于奥利奥 [重复]【英文标题】:Intent.ACTION_SEND not working on Oreo [duplicate] 【发布时间】:2018-07-24 16:28:26 【问题描述】:

我正在开发一个自定义相机应用程序,它可以捕获一张图片并将其存储在图库中。当我使用 Intent.ACTION_SEND 共享该图像时,它在所有设备上都运行良好,除了具有 API 26 的设备,即 OREO。

我分享图片的代码是:

Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.setType("image/jpeg");
            Uri uriShare = Uri.fromFile(outFile);
            //outfile is the path of the image stored in the gallery
            shareIntent.putExtra(Intent.EXTRA_STREAM, uriShare);
            startActivity(Intent.createChooser(shareIntent, ""));

谁能帮我解决这个问题?

【问题讨论】:

【参考方案1】:

如果targetSdkVersion 高于24,则FileProvider 用于授予访问权限。

创建一个xml文件(路径:res\xmlprovider_paths.xml

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

下的AndroidManifest.xml中添加一个Provider
 <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>

替换

Uri uri = Uri.fromFile(fileImagePath);

Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileImagePath);

你就完成了。

【讨论】:

包含在 中。 视频呢?相同的代码不适用于共享视频,但适用于共享图像。 完美答案。 100% 工作 对于现代开发,将:android:name="android.support.v4.content.FileProvider" 替换为 android:name="androidx.core.content.FileProvider"【参考方案2】:

使用 File Provider 获取 IMAGE URI 并为意图添加标志...仔细按照这些步骤操作.. android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData() 这自从牛轧糖以来已经更新了..

【讨论】:

shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);这会有帮助吗? 是的..通过我发布的链接..【参考方案3】:

更多详情

阅读本文

https://developer.android.com/reference/android/support/v4/content/FileProvider.html

源代码

https://drive.google.com/open?id=1vfO43dMSt096CMp6nrOJNl3fJAf6MPwG

create xml folder inside providers_path.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>



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.holostik.sharescreenshotexample">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.holostik.sharescreenshotexample.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>


    </application>

</manifest>

package com.holostik.sharescreenshotexample;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.holostik.sharescreenshotexample.share.ScreenshotType;
import com.holostik.sharescreenshotexample.share.ScreenshotUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;

public class MainActivity extends AppCompatActivity 
    int n;
    String photoPath;

    LinearLayout rootContent;
    ImageView iv_share;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rootContent = (LinearLayout) findViewById(R.id.rootContent);
        iv_share = (ImageView) findViewById(R.id.iv_share);

        iv_share.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
                    int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this,
                            Manifest.permission.CAMERA);

                    if (permissionCheck == PackageManager.PERMISSION_GRANTED) 
                        Log.e("MainActivity ", "P granted");

                        takeScreenshot(ScreenshotType.FULL);

                     else 
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]Manifest.permission.CAMERA,
                                        Manifest.permission.READ_EXTERNAL_STORAGE,
                                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                                , 1);
                    
                 else 
                    Log.e("MainActivity", "Lower Than MarshMallow");
                    takeScreenshot(ScreenshotType.FULL);
                

            
        );

    


    /*  Method which will take screenshot on Basis of Screenshot Type ENUM  */
    private void takeScreenshot(ScreenshotType screenshotType) 
        Bitmap b = null;
        switch (screenshotType) 
            case FULL:
                b = ScreenshotUtils.getScreenShot(rootContent);
                break;
            case CUSTOM:
                //If Screenshot type is CUSTOM
                break;
        

        //If bitmap is not null
        if (b != null) 
//            showScreenShotImage(b);//show bitmap over imageview
            Log.e("keshav", "bitmap is -- > " + b);

            SaveImage(b);

            shareScreenshot();
      /*      File saveFile = ScreenshotUtils.getMainDirectoryName(MainActivity.this);//get the path to save screenshot
            File file = ScreenshotUtils.store(b, "screenshot" + screenshotType + ".jpg", saveFile);//save the screenshot to selected path
            shareScreenshot(file);//finally share screenshot
            Log.e("file Path", String.valueOf(file));
*/

         else
            //If bitmap is null show toast message
            Toast.makeText(MainActivity.this, R.string.screenshot_take_failed, Toast.LENGTH_SHORT).show();

    

    private void SaveImage(Bitmap finalBitmap)
    
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
        myDir.mkdirs();
        Random generator = new Random();
        n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-" + n + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists()) file.delete();
        try 
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

         catch (Exception e) 
            e.printStackTrace();
        
    


    /* TODO  Show screenshot Bitmap */
//    private void showScreenShotImage(Bitmap b) 
//        imageView.setImageBitmap(b);
//    

    private void shareScreenshot()
    
        photoPath = Environment.getExternalStorageDirectory() + "/saved_images" + "/Image-" + n + ".jpg";
        File F = new File(photoPath);
        //Uri U = Uri.fromFile(F);
        //  Uri U = FileProvider.getUriForFile(getActivity(), getActivity().getApplicationContext().getPackageName() + ".my.package.name.provider", F);

        // TODO your package name as well add .fileprovider
        Uri U = FileProvider.getUriForFile(MainActivity.this.getApplicationContext(), "com.holostik.sharescreenshotexample.fileprovider", F);
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("image/png");
        i.putExtra(Intent.EXTRA_STREAM, U);
        startActivityForResult(Intent.createChooser(i, "Email:"), 1);
    

    // TODO Share Screen SHOT End ..............

【讨论】:

以上是关于Intent.ACTION_SEND 不适用于奥利奥 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

使用 Android Intent.ACTION_SEND 发送电子邮件

使用 Android Intent.ACTION_SEND 发送电子邮件

StartActivityForResults 总是为 Intent.ACTION_SEND 返回 RESULT_CANCELLED

如何通过Intent.ACTION_SEND分享Arraylist

Android:通过 Intent.ACTION_SEND 分享(某事),然后自动返回我的应用

Android - Intent.Action_Send 发送多个图像而不多次显示选择器对话框