gpt4 book ai didi

android - 如何与 viewpager 共享图像 Intent ?

转载 作者:行者123 更新时间:2023-11-29 16:12:35 25 4
gpt4 key购买 nike

我需要开发一个应用程序,当用户按下共享按钮时,我需要能够从ViewPager 中的ImageView共享图像 .

我的问题:

  • 当我按下共享按钮时,ImageView 缓存下一个Image 而不是当前的 .

    public class MainActivity extends Activity { private ViewPager pager; private ImageView Imgv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pager = (ViewPager) findViewById(R.id.pager);
    final Integer[] imageUrls={
    R.drawable.img1 , R.drawable.img2 , R.drawable.img3 , R.drawable.img4
    };
    pager.setAdapter(new MyAdapter(imageUrls));

    Button share = (Button) findViewById(R.id.share);
    share.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
    // TODO Auto-generated method stub
    Imgv.setDrawingCacheEnabled(true);
    Bitmap icon = Imgv.getDrawingCache();
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
    try {
    file.createNewFile();
    FileOutputStream fo = new FileOutputStream(file);
    fo.write(bytes.toByteArray());
    } catch (IOException e) {
    e.printStackTrace();
    }

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/jpeg");
    Uri uri = Uri.parse("file:///sdcard/temporary_file.jpg");
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
    startActivity(shareIntent);
    }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }
    private class MyAdapter extends PagerAdapter {

    private LayoutInflater inflater;
    private Integer[] fimg;

    public MyAdapter(Integer[] fimg ) {
    this.fimg=fimg;

    inflater = getLayoutInflater();

    }

    @Override
    public Object instantiateItem(View collection, int position ) {


    final View imageLayout = inflater.inflate(R.layout.imgview, null);

    Imgv = (ImageView)imageLayout.findViewById(R.id.full_image_view);





    Imgv.setBackgroundResource(fimg[position]);








    ((ViewPager) collection).addView(imageLayout ,0);


    return imageLayout;

    }




    @Override
    public int getItemPosition(Object object) {
    // TODO Auto-generated method stub
    return super.getItemPosition(object);
    }

    @Override
    public int getCount() {
    return fimg.length;
    }
    @Override
    public void destroyItem(View container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
    // TODO Auto-generated method stub
    return view==((View)object);
    }

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

    @Override
    public Parcelable saveState() {
    return null;
    }
    @Override
    public void startUpdate(View container) {
    }
    @Override
    public void finishUpdate(View container) {

    }

最佳答案

因为你认为 Imgv 是 ViewPager 中当前可见页面的 ImageView。但事实并非如此。在您的情况下,Imgv 是来自最后一个膨胀页面的 ImageView,但不是当前可见页面。

我建议你给 ImageView 添加标签:

Imgv.setBackgroundResource(fimg[position]);
Imgv.setTag(position); // add this line

然后你可以通过标签找到你的ImageView:

ImageView Imgv = (ImageView)pager.findViewWithTag(pager.getCurrentItem());
Imgv.setDrawingCacheEnabled(true);
...

关于android - 如何与 viewpager 共享图像 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11830435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com