共享动作提供程序以从imageView共享图像提供空指针异常

时间:2017-08-13 17:36:08

标签: java android nullpointerexception shareactionprovider

我尝试在此guide之后共享从幻灯片加载到imageview中的图像,它给了我一个空指针异常。我在下面发布了代码和堆栈跟踪,它

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)                                                                                                      at com.example.imnobody.sampleprojectnetwork.MainActivity.getBitmapFromDrawable(MainActivity.java:134)                                                                                                      at com.example.imnobody.sampleprojectnetwork.MainActivity.prepareShareIntent(MainActivity.java:95)                                                                                                      at com.example.imnobody.sampleprojectnetwork.MainActivity $ 1.onResourceReady(MainActivity.java:59)                                                                                                      at 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.access 100美元(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)                                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)                                                                                                      在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(本地方法)

1 个答案:

答案 0 :(得分:0)

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

com.example.imnobody.sampleprojectnetwork.fileprovider

这是您向getUriForFile()提供的内容,声称它是您的提供商的权限:

com.example.imnobody.sampleprojectnetwork

这些不一样,而且需要。