web-dev-qa-db-ja.com

Intent.ACTION_SENDがOreoで機能しない

写真をキャプチャしてギャラリーに保存するカスタムカメラアプリケーションを開発しています。 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, ""));

誰かがこの問題を解決するのを手伝ってくれる?

4
Ali Haider

targetSdkVersion24より大きい場合は、アクセスを許可するために 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.xmlProviderを追加します

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

およびreplace

Uri uri = Uri.fromFile(fileImagePath);

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

これで完了です。

28
Ankit Patidar

File Providerを使用してIMAGE URIを取得し、フラグをインテントに追加...これらの手順を慎重に実行してください。Android.os.FileUriExposedException:file:///storage/emulated/0/test.txt exposed through app through Intent.getData()この更新は、ヌガー..

3
Santanu Sur

詳細について

これを読む

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 ..............
}
0
Keshav Gera