با سلام
مشکل من اینه که عکس جاری نمایش داده شده در viewpager به اشتراک گذاشته نمیشه و عکس بعدی و در بعضی موارد عکس قبلی share میشه.
در مورد save از viewpager هم اگه ممکنه راهنماییم کنید.
بی نهایت سپاسگزارم
کد رو هم هر چقدر ویرایش میکنم اینطوری نمایش داده میشه.
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);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
final Integer[] imageUrls={
R.drawable.image_1,
R.drawable.image_2,
R.drawable.image_3,
R.drawable.image_4,
R.drawable.image_5,
R.drawable.image_6,
R.drawable.image_7,
R.drawable.image_8,
R.drawable.image_9,
R.drawable.image_10
};
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(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.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(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;
private ArrayList<View> views= new ArrayList<View>();
public MyAdapter(Integer[] fimg ) {
this.fimg=fimg;
inflater = getLayoutInflater();
}
@Override
public Object instantiateItem(View collection, int position ) {
// LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View imageLayout = inflater.inflate(R.layout.imgview, null);
Imgv = (ImageView)imageLayout.findViewById(R.id.full_image_view);
Imgv.setBackgroundResource(fimg[position]);
// Imgv.setTag(position);
((ViewPager) collection).addView(imageLayout ,0);
return imageLayout;
}
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
// Imgv = (ImageView)pager.findViewWithTag(pager.getCurrentItem());
// int index = views.indexOf(object) ;
// if (index== -1)
// return POSITION_NONE;
//else
// return index;
return super.getItemPosition(object);
}
public int getCount() {
return fimg.length;
}
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view==((View)object);
}
public View getView(int position ) {
return views.get(position);
}
}
}
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)