Wednesday, April 23, 2014

Sharing Picture to Social Media programatically in android


Give a Share intent to android from application and if the social media applications are installed on the device then the social media application will take care of that

Sharing to Facebook

Give the path of the photo
String mPath;//give your file path here
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+mPath));
share.setPackage("com.facebook.katana");
startActivityForResult(Intent.createChooser(share, "Share Image Via"),SHARE_PIC_REQUEST);


Sharing to Twitter

String mPath;//give your file path here
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+mPath));
share.putExtra(Intent.EXTRA_TEXT, "#androidtest");
share.putExtra(Intent.EXTRA_TITLE,"#androidtest" );
share.putExtra(Intent.EXTRA_SUBJECT,"#androidtest" );
share.setPackage("com.twitter.android");
startActivityForResult(Intent.createChooser(share, "Share Image Via"),SHARE_PIC_REQUEST);

Sharing to Instagram

String mPath;//give your file path here
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+mPath));
share.setPackage("com.instagram.android");
startActivityForResult(Intent.createChooser(share, "Share Image Via"),SHARE_PIC_REQUEST);

Sharing to google plus

String mPath;//give your file path here
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+mPath));
share.setPackage("com.google.android.apps.plus");
startActivityForResult(Intent.createChooser(share, "Share Image Via"),SHARE_PIC_REQUEST);

Dont Forget to give the permission
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


How to take the Screen shot in android and then share to Social Media

public static Bitmap loadBitmapFromView(Context context, View v) {
   DisplayMetrics dm = context.getResources().getDisplayMetrics();
   v.measure(MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.EXACTLY),
           MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.EXACTLY));
   v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
   Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
           v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
   Canvas c = new Canvas(returnedBitmap);
   v.draw(c);

   return returnedBitmap;
}

public String takeScreen(View view) {
   Bitmap bitmap = loadBitmapFromView(this, view); //get Bitmap from the view
   String mPath = Environment.getExternalStorageDirectory() + File.separator + "screen_" + System.currentTimeMillis() + ".jpeg";
   File imageFile = new File(mPath);

   try {
       OutputStream fout = new FileOutputStream(imageFile);
       bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
       fout.flush();
       fout.close();
   } catch (FileNotFoundException e) {
       e.printStackTrace();
   } catch (IOException e) {
       e.printStackTrace();
   }
   return mPath;

}



How to check Social Media Applications are installed or not

private boolean isPackageInstalled(String packagename, Context context) {
   PackageManager pm = context.getPackageManager();
   try {
       pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
       return true;
   } catch (NameNotFoundException e) {
       return false;
   }
}