0

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 ( );
    }
}}

1 Answers1

0

Uri.fromFile(...) can't work in nougat device instead of that try..

First Step:

<manifest ...>
<application ...>
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>

Then >>

create xml folder and file

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-pathname="external_files" path="."/>
</paths>

in coding change >>

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  Uri uri=Uri.fromFile(..);
}else{
      Uri uri= FileProvider.getUriForFile(
                             context, 
                             context.getApplicationContext()
                             .getPackageName() + ".provider", file);

}

And Try Solution of First question put shareimage function inside CustomSlide class and use there imageview only. try this....

private void ImageShare() {
 try {
  String cachepath = getCacheDir().getPath();
  imageView.setDrawingCacheEnabled(true);
  Bitmap viewc = Bitmap.createBitmap(imageView.getDrawingCache());
  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();
 }
}
Abhay Koradiya
  • 2,068
  • 2
  • 15
  • 40
  • how to put imageshare function in customslide? I can't fix it – Seema Kapoor Feb 01 '18 at 16:57
  • what is the Error?? – Abhay Koradiya Feb 01 '18 at 16:58
  • no actually its not about the error, I wrote the exact same as it is in mainactivity private void ImageShare() { this.imageView=(ImageView)findViewById ( R.id.image_view);} – Seema Kapoor Feb 01 '18 at 17:07
  • I have Edited my answer. Please Try that and let me know if any error. – Abhay Koradiya Feb 01 '18 at 17:17
  • application has stopped....error in "this.imageView.setDrawingCacheEnabled ( true );" – Seema Kapoor Feb 03 '18 at 16:09
  • E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.jindapps.gorukapoor.abc.MainActivity.ImageShare(MainActivity.java:56) at com.jindapps.gorukapoor.abc.MainActivity.onOptionsItemSelected(MainActivity.java:39) – Seema Kapoor Feb 03 '18 at 16:47
  • imageshare function adapter me hai?? – Abhay Koradiya Feb 03 '18 at 16:54
  • java.lang.NullPointerException at com.jindapps.gorukapoor.abc.MainActivity.ImageShare(MainActivity.java:56) at com.jindapps.gorukapoor.abc.MainActivity.onOptionsItemSelected(MainActivity.java:39) at android.app.Activity.onMenuItemSelected(Activity.java:2534) at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:368) at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195) – Seema Kapoor Feb 03 '18 at 17:00
  • at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108) at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:674) at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822) at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963) – Seema Kapoor Feb 03 '18 at 17:00
  • at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624) at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150) at android.view.View.performClick(View.java:4084) at android.view.View$PerformClick.run(View.java:16966) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) – Seema Kapoor Feb 03 '18 at 17:01
  • at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) – Seema Kapoor Feb 03 '18 at 17:01
  • sorry but imageshare function ko m add nhi kr paa rha – Seema Kapoor Feb 03 '18 at 17:03
  • imageview already added in instatiateitem and fir bhi usy add krna h to m aise add kr rha hu private void ImageShare(){ imageView=(ImageView)findViewbyId(imageView); } but findviewbyid red ho jata h – Seema Kapoor Feb 03 '18 at 17:21