Search Tutorials

Thursday 12 February 2015

Android app Share, Rate and Share screenshot options

Mostly android apps have share and rate options to promote app and some apps also use share screenshot option which captures whole screen of app’s current activity and prompts relevant apps which can share image. For sharing options, you must read about Implicit Intent first. In this post, I am going to share code for…

Android app Share, Rate and Share screenshot options

Share option:-

This option uses Implicit Intent to share promo text of application.

Intent i = new Intent("android.intent.action.SEND");
i.setType("text/plain");
i.putExtra("android.intent.extra.TEXT", "Enjoy multiple apps in a single small sized app. Download Helper+ App:- https://play.google.com/store/apps/details?id=helper.plus");
startActivity(Intent.createChooser(i, getResources().getString(Share via:)));

Rate option:-

This option also uses Implicit Intent to open page of your application on Google Play app/website from your app.

try{
 Intent i=new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=helper.plus"));
 startActivity(i);
}
catch(ActivityNotFoundException error){
 i=new Intent(Intent.ACTION_VIEW,Uri.parse("https://play.google.com/store/apps/details?id=helper.plus"));
 startActivity(i);
}

Share screenshot option:-

To capture screenshot of app’s current activity, we have to assign id to parent layout. For example: if I have below XML code for an activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/rl"
    android:orientation="vertical" >
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

ID of parent layout is “rl” which is used to capture screenshot. After taking screenshot of activity, we have to save screenshot as an image in our sdcard and after that we can share image via Implicit Intent to other apps. I made a class “ShareScreenshot” to capture and share screenshot. Simply, create an object and call method to use this functionality.

ShareScreenshot ss=new ShareScreenshot(MainActivity.this);
ss.shareImage();

ShareScreenshot.java file contents are:-

package helper.plus; //your package name

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.Toast;

public class ShareScreenshot {
 
 private Activity activity;
 
 public ShareScreenshot(Activity act){
  activity=act;
 }
 
 public void shareImage(){
  String path=Environment.getExternalStorageDirectory()+File.separator+"Screenshot.jpeg";
   File imageFile=new File(path);
  // create bitmap screen capture
  DisplayMetrics dm = activity.getResources().getDisplayMetrics(); 
  View v = activity.getWindow().getDecorView().findViewById(R.id.rl).getRootView();
     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);
 // v1.setDrawingCacheEnabled( false);
  OutputStream fout = null ;
  try {
      fout = new FileOutputStream(imageFile);
      returnedBitmap.compress(Bitmap.CompressFormat.JPEG, 90 , fout);
      fout.flush();
      fout.close();
      Toast.makeText(activity, "Image saved!", Toast.LENGTH_SHORT).show();
  } catch ( FileNotFoundException e) {
  // TODO Auto-generated catch block
   Toast.makeText(activity,"File not found!", Toast.LENGTH_SHORT).show();
     // e.printStackTrace();
  } catch (IOException e) {
  // TODO Auto-generated catch block
   Toast.makeText(activity, "IO Exception!", Toast.LENGTH_SHORT).show();
     // e.printStackTrace();
  }
  
     Intent i = new Intent();
     i.setAction(Intent.ACTION_SEND);
     i.setType("image/*");
     i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile( new File (path)));

     activity.startActivity(i);
 }
}

Because we are storing screenshot in sdcard so we have to add permission in android manifest xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Sharing and rating options increased your app popularity and download. Don't miss a chance to embed it in your app.

Related Tutorials:-

Create Menu using XML

Create Menu using Java

Access Call, Camera, Web pages

Open Second Activity using Intent

Open File from SDcard

No comments:

Post a Comment

Back to Top