0

I am new to android and I want share my image with the according position in my image storage array. I used an viewpage adopter so when a user swipes to the left or to the right, the imageview changes. Now I want share this which is a set of imageview code.

  package anil.card.christmas.christmascard;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
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 android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;

class ChristmasAdopter extends PagerAdapter {
    Context mContext;
    LayoutInflater mLayoutInflater;
    ImageView imageView;

    int imagepostion;
    private int[] mResources = {R.drawable.cs25, R.drawable.cs2,R.drawable.dx,R.drawable.prd, R.drawable.op,
            R.drawable.ud, R.drawable.ok,R.drawable.cs3,R.drawable.mux,R.drawable.ch, R.drawable.cs5,R.drawable.anil,
            R.drawable.cs6, R.drawable.cs7, R.drawable.pk, R.drawable.cs8, R.drawable.cs9,R.drawable.gk, R.drawable.cs10,
            R.drawable.cs11, R.drawable.cs13, R.drawable.cs14, R.drawable.cs15, R.drawable.cs16, R.drawable.cs17, R.drawable.cs19, R.drawable.cs21, R.drawable.cs23, R.drawable.cs1, R.drawable.cs27, R.drawable.cs29, R.drawable.cs31, R.drawable.cs33,
            R.drawable.cs35,R.drawable.newyear,R.drawable.ny,R.drawable.mn,R.drawable.hp,R.drawable.lifenew,

    R.drawable.cat,R.drawable.dog,R.drawable.flower,R.drawable.ws,R.drawable.chars,
    R.drawable.ship,R.drawable.bells,R.drawable.gift,R.drawable.dj};

    public ChristmasAdopter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

        imageView = (ImageView) itemView.findViewById(R.id.imageView);

      imagepostion=position;
    imageView.setImageResource(mResources[position]);

    container.addView(itemView);

        return itemView;
    }


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


// here is share content please tell me how i can share image which is show in imageView
    public void shareContent(){

        Toast.makeText(mContext, ""+imagepostion, Toast.LENGTH_SHORT).show();



    }

    private Bitmap getBitmapFromView(View view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null) {
            bgDrawable.draw(canvas);
        }   else{
            canvas.drawColor(Color.WHITE);
        }
        view.draw(canvas);
        return returnedBitmap;
    }

}



    }
Twenty
  • 5,234
  • 4
  • 32
  • 67
joshua
  • 177
  • 1
  • 2
  • 14
  • https://stackoverflow.com/questions/9049143/android-share-intent-for-a-bitmap-is-it-possible-not-to-save-it-prior-sharing?rq=1 – Peter Alwin Nov 26 '19 at 06:31
  • @PeterAlwin please check my code – joshua Nov 26 '19 at 06:57
  • how about using recyclerview for this? can you try that one. – Peter Alwin Nov 26 '19 at 07:00
  • its not recylerview its pageAdopter and viewpager for single image when user swipe left and right it show image on imageView so user chose any image share with friends now i have problem sharing image only – joshua Nov 26 '19 at 07:03
  • Please be clear on where to share the image to. If you mention that, only then people will be able to help you out. There are various sharing options. You can share the image to social-media from your app(i.e. facebook, whatsapp, etc.). Or you could share it with a server via post apis.etc. There are many other options too. Please clarify, what do you want to do. – Karthik Pai Nov 26 '19 at 09:01
  • @joshua The link that Peter Alwin provided is right. It is helpful. – Karthik Pai Nov 26 '19 at 09:11
  • @KarthikPai i want share it on social media simple intent its just like share image from imageview nothing more – joshua Nov 26 '19 at 09:47
  • @joshua then you can share the image on clicking ImageView. Use imageView.setOnClickListener() method. – Karthik Pai Nov 26 '19 at 09:52
  • @KarthikPai i have viewpager when user swipe it change image in ImageView now i want share this image which is set in image the image in my array every time user swipe image is set new image in imageview so the image which set in imageview i want share that image? – joshua Nov 26 '19 at 09:56
  • @joshua In your ChristmasAdopter, instantiateItem(ViewGroup container, int position) method, you can write imageView.setOnClickListener(new View.OnClickListener()) below imageView = (ImageView) itemView.findViewById(R.id.imageView); statement. – Karthik Pai Nov 26 '19 at 10:03
  • @KarthikPai just tell me how to share image from imageview on social media using file provider simple? – joshua Nov 26 '19 at 10:10

1 Answers1

0

In your ChristmasAdopter class, replace instantiateItem() with,

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

    imageView = (ImageView) itemView.findViewById(R.id.imageView);

  imagepostion=position;
  imageView.setImageResource(mResources[position]);

  imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//save image to device
      saveImage();
//share image code
      shareImageFromDevice();
    }
    });
    container.addView(itemView);

    return itemView;
}

private void saveImage(){

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),mResources[position]);

File image = new File(getExternalFilesDir(null), "test.png");
// After that, you just have to write the Bitmap thanks to its method compress such as:

boolean success = false;

// Encode the file as a PNG image.
FileOutputStream outStream;
try {

    outStream = new FileOutputStream(image);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
    /* 100 to keep full quality of the image */

    outStream.flush();
    outStream.close();
    success = true;
} catch (IOException e) {
    e.printStackTrace();
}
//Finally, just deal with the boolean result if needed. Such as:

if (success) {
    Toast.makeText(getApplicationContext(), "Image saved with success",
            Toast.LENGTH_LONG).show();
} else {
    Toast.makeText(getApplicationContext(),
            "Error during image saving", Toast.LENGTH_LONG).show();
}
}
    private void shareImageFromDevice(){

    File file = new File(getExternalFilesDir(null), "test.png");

    Uri uri=Uri.fromFile(file);

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Share Image"));
}
Karthik Pai
  • 587
  • 1
  • 9
  • 18
  • I have not used FileProvider. But I have done it in a different way. – Karthik Pai Nov 26 '19 at 12:09
  • Now in the app, When you swipe right or swipe left, the image will change. And when you touch the image , a chooser dialog screen will come up so that you can share the image. There were many links available over the net. If you are not comfortable with one you can refer to another. This easy research should have been done by you yourself. – Karthik Pai Nov 26 '19 at 12:17
  • it says sharing failed try again i send on whatsapp? – joshua Nov 26 '19 at 13:53
  • @joshua Few things have changed since Android API 29. This is an updated code. You need to first save the file to device then you share it. I have also tested it. This should work. – Karthik Pai Nov 29 '19 at 10:16