android taking the screenshot and sharing it

regeme picture regeme · Oct 7, 2013 · Viewed 11.6k times · Source

I am trying to add a share button to my app which takes the screenshot of screen and then shares it via Facebook etc. I have searched over internet. I also have searched Stack Overflow too; there are many threads related with this issue but I couldn't figure that out yet. What is confusing me is .. In every example the image's filepath is hardcoded. I did like that but it is not useful and dynamic. What I am trying to do is to take the screenshot of that moment and then share it, but when I give the filepath by myself it just takes that picture from a folder and shares that

public void clickButton(View v) {
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);

    //set the type  
    shareIntent.setType("image/png");

    //add a subject  
    shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "CAR EXAMPLE");

    //build the body of the message to be shared  
    String shareMessage = "An app...";

    //add the message  
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareMessage);

    //add the img
    shareIntent.putExtra(Intent.EXTRA_STREAM,
            Uri.parse("/storage/sdcard0/Tutorial_ScreenShot/screenshot0.jpg"));

    //start the chooser for sharing  
    startActivity(Intent.createChooser(shareIntent, "Share"));
}

As you can see in the add image part I give the filepath by myself. So how to give a more dynamic behaviour to that .. I mean when I click the button my app's screen has to be saved in a folder and then I can share that without hardcoding its filepath.

Edited: After trying the solution below;

Well, thanks. I've called shareIt() right under onClick of a button it and application stops... Here is the log.

12-28 15:53:01.660: E/AndroidRuntime(14120): FATAL EXCEPTION: main
12-28 15:53:01.660: E/AndroidRuntime(14120): Process: com.hede.namesurfer, PID: 14120   
12-28 15:53:01.660: E/AndroidRuntime(14120): java.lang.NullPointerException
12-28 15:53:01.660: E/AndroidRuntime(14120):    at      
com.hede.namesurfer.MainActivity.share(MainActivity.java:161)
12-28 15:53:01.660: E/AndroidRuntime(14120):    at  
  com.hede.namesurfer.MainActivity$1.onClick(MainActivity.java:42)
12-28 15:53:01.660: E/AndroidRuntime(14120):    at 
android.view.View.performClick(View.java:4438)
12-28 15:53:01.660: E/AndroidRuntime(14120):    at 
android.view.View$PerformClick.run(View.java:18422)
12-28 15:53:01.660: E/AndroidRuntime(14120):    at 
android.os.Handler.handleCallback(Handler.java:733)

12-28 15:53:01.660: E/AndroidRuntime(14120):    at  
android.os.Handler.dispatchMessage(Handler.java:95)
12-28 15:53:01.660: E/AndroidRuntime(14120):    at 
android.os.Looper.loop(Looper.java:136)
12-28 15:53:01.660: E/AndroidRuntime(14120):    at 
android.app.ActivityThread.main(ActivityThread.java:5017)
12-28 15:53:01.660: E/AndroidRuntime(14120):    at 
java.lang.reflect.Method.invokeNative(Native Method)
12-28 15:53:01.660: E/AndroidRuntime(14120):    at
java.lang.reflect.Method.invoke(Method.java:515)
12-28 15:53:01.660: E/AndroidRuntime(14120):    at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-28 15:53:01.660: E/AndroidRuntime(14120):    at    
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

12-28 15:53:01.660: E/AndroidRuntime(14120):    at    
dalvik.system.NativeStart.main(Native Method)
12-28 15:53:02.780: I/Process(14120): Sending signal. PID

Answer

Singhak picture Singhak · Dec 24, 2013

Try this

public void shareit()
    {
        View view =  findViewById(R.id.layout);//your layout id
        view.getRootView();
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) 
        {
            File picDir  = new File(Environment.getExternalStorageDirectory()+ "/myPic");
            if (!picDir.exists())
            {
                picDir.mkdir();
            }
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache(true);
            Bitmap bitmap = view.getDrawingCache();
//          Date date = new Date();
            String fileName = "mylove" + ".jpg";
            File picFile = new File(picDir + "/" + fileName);
            try 
            {
                picFile.createNewFile();
                FileOutputStream picOut = new FileOutputStream(picFile);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), (int)(bitmap.getHeight()/1.2));
                boolean saved = bitmap.compress(CompressFormat.JPEG, 100, picOut);
                if (saved) 
                {
                    Toast.makeText(getApplicationContext(), "Image saved to your device Pictures "+ "directory!", Toast.LENGTH_SHORT).show();
                } else 
                {
                    //Error
                }
                picOut.close();
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
            view.destroyDrawingCache();

            // share via intent
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("image/jpeg");
            sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(picFile.getAbsolutePath()));
            startActivity(Intent.createChooser(sharingIntent, "Share via"));
        } else {
            //Error

        }
    }