图像共享无效

时间:2017-03-11 06:10:00

标签: android

在我的项目中有一个图库并且点击了一张照片,它会放大并打开一个带有共享选项的片段。

我正在尝试分享照片,但是当我按下分享按钮时应用程序崩溃了。它在logcat中显示了nullpoint异常。这是共享代码

public class MyViewPagerAdapter extends PagerAdapter {

    private LayoutInflater layoutInflater;

    public MyViewPagerAdapter() {
    }



    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.image_fullscreen, container, false);




        imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);
        FloatingActionButton b1 = (FloatingActionButton) view.findViewById(R.id.Img_share);

        Item_album image = images.get(position);


        Glide.with(getActivity()).load(image.getImage())
                .thumbnail(1f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageViewPreview);

        container.addView(view);

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

                Bitmap b = BitmapFactory.decodeResource(getResources(), R.id.image_preview);

                Intent share = new Intent(Intent.ACTION_SEND);

                share.setType("image/jpeg");

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                String path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), b, "Title", null);
                Uri imageUri = Uri.parse(path);
                share.putExtra(Intent.EXTRA_STREAM, imageUri);
                startActivity(Intent.createChooser(share, "Select"));


            }
        });



        return view;
    }

这是logcat中的错误

FATAL EXCEPTION: main
                                                                          Process: com.touchlive.info.paramount, PID: 14744
                                                                          java.lang.NullPointerException
                                                                              at com.touchlive.info.paramount.SlideshowDialogFragment$MyViewPagerAdapter$2.onClick(SlideshowDialogFragment.java:230)
                                                                              at android.view.View.performClick(View.java:4446)
                                                                              at android.view.View$PerformClick.run(View.java:18437)
                                                                              at android.os.Handler.handleCallback(Handler.java:733)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:136)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5372)
                                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:515)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
                                                                              at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我在我的应用程序中做了完全相同的事情。这是我使用的代码,

Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable. image_preview);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
                    b, "Title", null);

Uri imageUri =  Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));

我也碰巧使用了这个权限,

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