如何共享GridView中的图像?

时间:2015-12-07 11:44:42

标签: android gridview

我想分享网格视图中的图像。当我在网格视图中长按图像时,我希望特定图像共享。我尝试发送“文本”,我能够发送它,但是,当我尝试发送图像时,它抛出空指针异常。我检查了我的代码,但我不知道我错过了哪里。

这是我的代码,

private GridView grid;
Context context;

private Animator mCurrentAnimator;
private int thumb[] = {
        R.drawable.tcdhspecs, R.drawable.talspec, R.drawable.tgpspecs, R.drawable.ttospecs, R.drawable.tglspecs, R.drawable.thdspec};
private ImageButton expandedImageView;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

private int mShortAnimationDuration;
private int j = 0;
String[] web = {

        "VETHA TCDH",
        "VETHA TAL",
        "VETHA TGP",
        "VETHA TTO",


};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_main_pinch_vetha, container, false);

    Customgrid_pinch_vetha adapter = new Customgrid_pinch_vetha(getContext(), web, thumb);

    grid = (GridView) rootView.findViewById(R.id.gridView);
    grid.setAdapter(adapter);
    expandedImageView = (ImageButton) rootView.findViewById(R.id.expanded_image);
    expandedImageView.setLayoutParams(new FrameLayout.LayoutParams((int) (100), 100));
    ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(expandedImageView.getLayoutParams());
    marginParams.setMargins((int) (100 / 1.5), 0, 0, 0);
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(marginParams);
    expandedImageView.setLayoutParams(layoutParams);

  System.out.println(thumb[pos]);

        }
    });

          android.R.integer.config_shortAnimTime);

    registerForContextMenu(grid);
    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getActivity().getMenuInflater();             
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
        case R.id.share_fav:
  BitmapDrawable bm = (BitmapDrawable) expandedImageView.getDrawable();
            Bitmap mysharebmp = bm.getBitmap();
            String path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),
                    mysharebmp, "MyImage", null);
            Uri uri = Uri.parse(path);

            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("image");

            sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
            startActivity(Intent.createChooser(sharingIntent,
                    "Share image using"));


        default:
            return super.onContextItemSelected(item);
    }


}


@Override
public void onDestroyView() {
    super.onDestroyView();
}
}

我的日志..

  12-07 17:25:58.049 11486-11486/app.dme.anusha.dmeapplication    E/AndroidRuntime: FATAL EXCEPTION: main
  12-07 17:25:58.049 11486-11486/app.dme.anusha.dmeapplication  E/AndroidRuntime: java.lang.NullPointerException
  12-07 17:25:58.049 11486-11486/app.dme.anusha.dmeapplication E/AndroidRuntime:     at   app.dme.anusha.dmeapplication.SpecTabsVetha.onContextItemSelected(SpecTabsVetha.java:201)
 12-07 17:25:58.049 11486-11486/app.dme.anusha.dmeapplication E/AndroidRuntime:     at android.support.v4.app.Fragment.performContextItemSelected(Fragment.java:2085)
 12-07 17:25:58.049 11486-11486/app.dme.anusha.dmeapplication E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.dispatchContextItemSelected(FragmentManager.java:2132)
12-07 17:25:58.049 11486-11486/app.dme.anusha.dmeapplication E/AndroidRuntime:     at android.support.v4.app.FragmentController.dispatchContextItemSelected(FragmentController.java:308)

这是我的代码。当我尝试发送网格视图中的图像时,它会抛出空指针异常。请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

不是通过expandedImageView.getDrawable()访问图片,而是在setTag()中设置图片的路径,然后尝试访问图片。

图像可能大或小。 expandedImageView.getDrawable()可以为您提供空值。