Here in image slider with viewpager app when I want to share 5th image but after opening WhatsApp or other app there is 4th image attached. This is the main problem and another problem is it doesn't work with android 7 or more, there WhatsApp attachment is going to blank only black background.
Custom Slide....
public class CustomSlide extends PagerAdapter{
private int[] image_resources={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,
R.drawable.i,R.drawable.j,R.drawable.k,R.drawable.l,R.drawable.m,R.drawable.n,R.drawable.o,R.drawable.p,R.drawable.q,R.drawable.r,R.drawable.s,
R.drawable.t,R.drawable.u,R.drawable.v,R.drawable.w,R.drawable.x,R.drawable.y,R.drawable.z};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomSlide(Context ctx)
{this.ctx=ctx;}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater= (LayoutInflater) ctx.getSystemService ( Context.LAYOUT_INFLATER_SERVICE );
View itview=layoutInflater.inflate ( R.layout.slide,container,false );
ImageView imageView=(ImageView) itview.findViewById ( R.id.image_view );
imageView.setImageResource ( image_resources[position] );
container.addView ( itview );
return itview;
}
@Override
public int getCount() {
return image_resources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==(LinearLayout)object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView ( (LinearLayout) object );
}}
MainActivity....
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
CustomSlide customSlide;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
viewPager=(ViewPager) findViewById ( R.id.view_pager );
customSlide=new CustomSlide ( this );
viewPager.setAdapter ( customSlide );
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId ()){
case R.id.Sending:
ImageShare();
return true;
}
return super.onOptionsItemSelected ( item );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater ().inflate ( R.menu.main, menu );
return true;
}
private void ImageShare() {
try{
this.imageView=(ImageView)findViewById ( R.id.image_view);
String cachepath=getCacheDir ().getPath ();
this.imageView.setDrawingCacheEnabled ( true );
Bitmap viewc=Bitmap.createBitmap ( this.imageView.getDrawingCache () );
this.imageView.setDrawingCacheEnabled ( false );
viewc.compress ( Bitmap.CompressFormat.JPEG,100,new FileOutputStream ( new StringBuilder ( String.valueOf ( cachepath ) ).append ( "image.jpg" ).toString () ) );
Intent share=new Intent ( Intent.ACTION_SEND );
share.setType ( "image/*" );
share.putExtra ( Intent.EXTRA_STREAM, Uri.fromFile (new File ( new StringBuilder ( String.valueOf ( cachepath ) ).append ( "image.jpg" ).toString ())));
startActivity ( Intent.createChooser ( share,"Send Via" ) );
} catch (FileNotFoundException e) {
e.printStackTrace ( );
}
}}