我需要开发一个应用程序,share image
当ImageView in ViewPager
用户press on Share Button
.
我的问题:
当我缓存下一个不是当前的
press on Share Button
.ImageView
Image
公共类 MainActivity 扩展 Activity { 私人 ViewPager 寻呼机; 私有 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) { }
}
}