0

我有一个ViewPagerwith an ImageView,我想让它这样当有人点击图片时,他们可以用 share 分享它Intent

我不知道该怎么做。下面是我的代码。

如果有人可以告诉我如何做到这一点,那就太好了!

public class ImagePagerActivity extends BaseActivity {

private ViewPager pager;
private TextView mSelected;
private String[] mLocations;
private DisplayImageOptions options;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ac_image_pager);

    mSelected = (TextView)findViewById(R.id.text);

    mLocations = getResources().getStringArray(R.array.locations);

    Context context = getSupportActionBar().getThemedContext();
    ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
    list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(list, this);
    getSupportActionBar().setSelectedNavigationItem(1);

    Bundle bundle = getIntent().getExtras();
    String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
    int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

    options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.image_for_empty_url)
        .cacheOnDisc()
        .imageScaleType(ImageScaleType.EXACT)
        .build();

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new ImagePagerAdapter(imageUrls));
    pager.setCurrentItem(pagerPosition);
}

@Override
protected void onStop() {
    imageLoader.stop();
    super.onStop();
}

private class ImagePagerAdapter extends PagerAdapter {

    private String[] images;
    private LayoutInflater inflater;

    ImagePagerAdapter(String[] images) {
        this.images = images;
        inflater = getLayoutInflater();
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View container) {
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object instantiateItem(View view, int position) {
        final FrameLayout imageLayout = (FrameLayout) inflater.inflate(R.layout.item_pager_image, null);
        final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

        imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted() {
                spinner.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(FailReason failReason) {
                String message = null;
                switch (failReason) {
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                }
                Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

                spinner.setVisibility(View.GONE);
                imageView.setImageResource(android.R.drawable.ic_delete);
            }

            @Override
            public void onLoadingComplete() {
                spinner.setVisibility(View.GONE);
                Animation anim = AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.fade_in);
                imageView.setAnimation(anim);
                anim.start();
            }

            @Override
            public void onLoadingCancelled() {
                // Do nothing
            }
        });

        ((ViewPager) view).addView(imageLayout, 0);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {
    }
}

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {

    if (itemPosition == 0){
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(intent);
    }

    getSupportActionBar().setSelectedNavigationItem(1);
    return true;
}   
}
4

1 回答 1

0

在您的对象instantiateItem方法中添加此代码

imageView.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/jpeg");
    intent.putExtra(Intent.EXTRA_STREAM, [image URI]);
    startActivity(Intent.createChooser(intent, "Share image with"));
  }
} 

这是你的代码的更新版本

public class ImagePagerActivity extends BaseActivity {

private ViewPager pager;
private TextView mSelected;
private String[] mLocations;
private DisplayImageOptions options;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ac_image_pager);

    mSelected = (TextView)findViewById(R.id.text);

    mLocations = getResources().getStringArray(R.array.locations);

    Context context = getSupportActionBar().getThemedContext();
    ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
    list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(list, this);
    getSupportActionBar().setSelectedNavigationItem(1);

    Bundle bundle = getIntent().getExtras();
    String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
    int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

    options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.image_for_empty_url)
        .cacheOnDisc()
        .imageScaleType(ImageScaleType.EXACT)
        .build();

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new ImagePagerAdapter(imageUrls));
    pager.setCurrentItem(pagerPosition);
}

@Override
protected void onStop() {
    imageLoader.stop();
    super.onStop();
}

private class ImagePagerAdapter extends PagerAdapter {

    private String[] images;
    private LayoutInflater inflater;

    ImagePagerAdapter(String[] images) {
        this.images = images;
        inflater = getLayoutInflater();
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View container) {
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object instantiateItem(View view, int position) {
        final FrameLayout imageLayout = (FrameLayout) inflater.inflate(R.layout.item_pager_image, null);
        final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

        imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted() {
                spinner.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(FailReason failReason) {
                String message = null;
                switch (failReason) {
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                }
                Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

                spinner.setVisibility(View.GONE);
                imageView.setImageResource(android.R.drawable.ic_delete);
            }

            @Override
            public void onLoadingComplete() {
                spinner.setVisibility(View.GONE);
                Animation anim = AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.fade_in);
                imageView.setAnimation(anim);
                anim.start();
            }

            @Override
            public void onLoadingCancelled() {
                // Do nothing
            }
        });

    //Changes START

    imageView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        //Create new Intent to send image
        Intent intent = new Intent(Intent.ACTION_SEND);
        //Set Intent Type
        intent.setType("image/jpeg");
        //Create URI for image link
        Uri uri = Uri.parse(images[position]);
        //Set Intent extra with URI created from image link
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        //Start new activity to share image
        startActivity(Intent.createChooser(intent, "Share image with"));
      }
    } 

    //Changes END

        ((ViewPager) view).addView(imageLayout, 0);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {
    }
}

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {

    if (itemPosition == 0){
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(intent);
    }

    getSupportActionBar().setSelectedNavigationItem(1);
    return true;
}   
}
于 2012-07-20T06:17:23.373 回答