从 imageView 共享图像的共享操作提供程序给出空指针异常

Share Action provider to share image from imageView gives null pointer exception

我试图分享一张从滑行加载到图像视图中的图像 guide,它给了我一个空指针异常。我已经在下面发布了代码和堆栈跟踪,

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private ShareActionProvider myShareActionProvider;
    //private Bitmap bitmap;
    private Uri uri;
    private Intent shareIntent;

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

        final ImageView imageView = (ImageView) findViewById(R.id.imageView);

        String hi = "http://37n98a43dqtb4bua9n28nidp.wpengine.netdna-cdn.com/wp-content/uploads/2016/09/MyFriendPikachu.jpg";

        Glide
                .with(this)
                .load(hi)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        prepareShareIntent(((GlideBitmapDrawable) resource).getBitmap());
                        attachShareIntentAction();
                        return false;
                    }
                })
                .placeholder(R.drawable.ic_action_name)
                .error(R.drawable.ic_img_error)
                .centerCrop()
                .into(imageView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.img_menu, menu);

        MenuItem item = menu.findItem(R.id.action_share);

        myShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
        attachShareIntentAction();

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        return super.onOptionsItemSelected(item);


    }

    public void prepareShareIntent(Bitmap drawableImage) {

        Uri bmpUri = getBitmapFromDrawable(drawableImage);

        shareIntent = new Intent();

        shareIntent.setAction(Intent.ACTION_SEND);

        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);

        shareIntent.setType("image/*");

    }


    public void attachShareIntentAction() {

        if (myShareActionProvider != null && shareIntent != null)

            myShareActionProvider.setShareIntent(shareIntent);

    }


    public Uri getBitmapFromDrawable(Bitmap bmp) {


        Uri bmpUri = null;

        try {


            File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "images" + System.currentTimeMillis() + ".png");

            FileOutputStream out = new FileOutputStream(file);

            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);

            out.close();


            bmpUri = FileProvider.getUriForFile(MainActivity.this, "com.example.imnobody.sampleprojectnetwork", file);  // use this version for API >= 24

            // **Note:** For API < 24, you may use bmpUri = Uri.fromFile(file);

        } catch (IOException e) {

            e.printStackTrace();

        }

        return bmpUri;

    }

}

fileprovider.xml

<?xml version="1.0" encoding="utf-8"?>

<paths>

    <external-file-path

        name="images"

        path="Pictures" />

</paths>

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />





    <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.example.imnobody.sampleprojectnetwork.fileprovider"

            android:exported="false"

            android:grantUriPermissions="true">

            <meta-data


                android:name="android.support.FILE_PROVIDER_PATHS"

                android:resource="@xml/fileprovider" />

        </provider>
    </application>

</manifest>

堆栈跟踪

java.lang.NullPointerException 在 android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583) 在 android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557) 在 android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399) 在 com.example.imnobody.sampleprojectnetwork.MainActivity.getBitmapFromDrawable(MainActivity.java:134) 在 com.example.imnobody.sampleprojectnetwork.MainActivity.prepareShareIntent(MainActivity.java:95) 在 com.example.imnobody.sampleprojectnetwork.MainActivity$1.onResourceReady(MainActivity.java:59) 在 com.example.imnobody.sampleprojectnetwork.MainActivity$1.onResourceReady(MainActivity.java:51) 在 com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:522) 在 com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:507) 在 com.bumptech.glide.load.engine.EngineJob.handleResultOnMainThread(EngineJob.java:158) 在 com.bumptech.glide.load.engine.EngineJob.access100 美元(EngineJob.java:22) 在 com.bumptech.glide.load.engine.EngineJob$MainThreadCallback.handleMessage(EngineJob.java:202) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:5292) 在 java.lang.reflect.Method.invokeNative(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 在 dalvik.system.NativeStart.main(本机方法)

您的 <provider> 具有以下 android:authorities 值:

com.example.imnobody.sampleprojectnetwork.fileprovider

这是您提供给 getUriForFile() 的内容,并声称这是您的提供者的授权:

com.example.imnobody.sampleprojectnetwork

这些是不一样的,他们需要是一样的。