Android takes a screenshot and shares it

I am trying to add a share button to my application that takes a screenshot and then shares it via Facebook, etc. I searched over the internet. I also searched for stack overflow; There are many threads related to this problem, but I still could not figure it out. What scares me: in each example, the image file path is hard-coded. I liked it, but it is not useful and dynamic. What I'm trying to do is take a screenshot of this moment and then share it, but when I give the path to the file myself, it just takes this image from the folder and splits it

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"));
}

, . , . , , , .

: ;

, . shareIt() OnClick, ... .

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
+4
4

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();
        } else {
            //Error

        }




        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"));
    }
+1

screeshot

 public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache(); }

private void saveBitmap(Bitmap bitmap) {
imagePath = new File(Environment.getExternalStorageDirectory() + "/scrnshot.png"); ////File imagePath
FileOutputStream fos;
try {
    fos = new FileOutputStream(imagePath);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
    Log.e("GREC", e.getMessage(), e);
}}

private void shareIt() {
Uri uri = Uri.fromFile(imagePath);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
String shareBody = "My highest score with screen shot";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Catch score");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(sharingIntent, "Share via"));}

shareScoreCatch.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bitmap bitmap = takeScreenshot();
                            saveBitmap(bitmap);
                            shareIt();
                        }
                    });

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

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

For those who may need a solution in the future, this is what I use in my applications. Tested and works on Android 4.0 to 7.1.

First, define the variable for the visible screen in your Office:

    public View preView;

Then run a view to allow a screenshot:

    preView = getWindow().getDecorView();

Then you can call the next method in your activity by pressing the button. I put this method in a headless class and refer to it as needed.

public static void takeScreenShotAndShare(final Context context, View view, boolean incText, String text){
    try{

        File mPath = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "screenshot.png");
        //File imageDirectory = new File(mPath, "screenshot.png");

        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);

        FileOutputStream fOut = new FileOutputStream(mPath);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.PNG, quality, fOut);
        fOut.flush();
        fOut.close();

        final Intent shareIntent = new Intent(Intent.ACTION_SEND);
        Uri pictureUri = Uri.fromFile(mPath);
        shareIntent.setType("image/*");
        if(incText){
            shareIntent.putExtra(Intent.EXTRA_TEXT, text);
        }
        shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(Intent.createChooser(shareIntent, "Share image using"));
    }catch (Throwable tr){
        Log.d(TAG, "Couldn't save screenshot", tr);
    }

}
0
source

All Articles