Search code examples
androidandroid-studioandroid-imageviewandroid-sharing

Why the Image can't be shared?


here in this app the viewPager contains some images, i want it to be shared but its showing error in sharing I am sharing my codes here this the Mainactivity.java file

package com.adixpc.hp.physicsfacts;



import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class Main2Activity extends AppCompatActivity {
ViewPager viewPager;
scrAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);


    viewPager = (ViewPager)findViewById(R.id.vp);

    adapter = new scrAdapter(this);

    viewPager.setAdapter(adapter);
      }


   }

and this one is the srcAdapater.java file

package com.adixpc.hp.physicsfacts;


import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import uk.co.senab.photoview.PhotoViewAttacher;

import static com.adixpc.hp.physicsfacts.R.id.imageView;


public class scrAdapter extends PagerAdapter {



private int image_source [] = {R.drawable.aa,
        R.drawable.bb,
        R.drawable.cc,
        R.drawable.dd,
        R.drawable.ee,
        R.drawable.ff,
        R.drawable.gg,
        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 scrAdapter (Context ctx){
    this.ctx= ctx;

}

@Override
public int getCount() {
    return image_source.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return (view==(LinearLayout)object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item = layoutInflater.inflate(R.layout.scrshots,container,false);
    ImageView img = item.findViewById(imageView);
    img.setImageResource(image_source[position]);
    img.setLongClickable(true);
    img.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_STREAM,"");
            intent.setType("image/jpeg");
            ctx.startActivity(intent);
            return true;
        }
    });

    container.addView(item);
    return item;


}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((LinearLayout)object);
}

}

when i try to long click one of the image to share it with other apps but when i selected any of them then it's showing "sharing failed,try again" what is wrong in my code plz resolve . Please upadate the codes that i can share all the images that are present in the image_source[] thank you


Solution

  • You didn't put anything to send via intent. You have to put the image in temporary path so other applications can access it and send uri path via intent. Check this answer for details.