How to share image in android progamatically , Sometimes you wants to take a snapshot of your view and then like to share it so follow these steps: 1.Add permission to mainfest file
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.Very First take a screenshot of your view , for example it is Imageview, Textview, Framelayout,LinearLayout etc
For example you have an image view to take screenshot call this method in oncreate()
ImageView image= (ImageView)findViewById(R.id.iv_answer_circle);
///take a creenshot
screenShot(image);
after taking screenshot call share image method either on button
click or where you wants
shareBitmap(screenShot(image),"myimage");
After on create method define these two method ##
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
//////// this method share your image
private void shareBitmap (Bitmap bitmap,String fileName) {
try {
File file = new File(getContext().getCacheDir(), fileName + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent( android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}