HDTutorial
EN

Playback starts YouTube’s embedded player (privacy-enhanced mode).

How to take screenshot from your Android App and open the image file programmatically?

11,427 views December 1, 2019 19:14

Watch on YouTube
ChannelProgrammer World

Description from the creator

This video shows the steps to take screenshots of the phone's screen area and save it as an image file at the desired file path. For image file it considers current data and time as unique name of the file. Further it shows how you can open the captured image file which can be shared over email, whatsapp, drive or bluetooth. All these steps are done programmatically and hence these APIs can be used to automate the task or control the screenshot remotely via SMS or some notifications.

We hope you like this video. For any query, suggestions or appreciations we will be glad to hear from you at: [email protected] or visit us at: programmerworld.co

programmerworld.co/android/how-to-take-screensh…

Source Code:
package com.example.myscreenshotshotapp;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(this,new String[]{WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}

public void ScreenshotButton(View view){
View view1 = getWindow().getDecorView().getRootView();
view1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view1.getDrawingCache());
view1.setDrawingCacheEnabled(false);

String filePath = Environment.getExternalStorageDirectory()+"/Download/"+ Calendar.getInstance().getTime().toString()+".jpg";
File fileScreenshot = new File(filePath);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(fileScreenshot);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(fileScreenshot);
intent.setDataAndType(uri,"image/jpeg");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
}

About this tutorial

Programmer World published this 19:14 video tutorial on December 1, 2019. It has been watched 11,427 times on YouTube.

Category
Education
Duration
19:14
Published
December 1, 2019
Views
11,427
Likes
98

Tags

Share

This tutorial in other languages