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

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: programmerworld1990@gmail.com or visit us at: https://programmerworld.co

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

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.myscreenshotshotapp”>

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

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

2 comments

  1. I assume this error is being thrown while trying to open the media file using the StartActivity(intent). Most likely this error is because your Media file, with screenshot image, is not getting created at the mentioned location.

    Browse through the file explorer and check if the file is getting created. If not then issue is with the screenshot capture it self. Try changing the location of your destination folder and see if it works. Also, ensure that your destination folder is not write protected by any other application on the phone.
    I assume you have given READ and WRITE permissions for external storage in your App.

    Good Luck
    Programmer World
    https://programmerworld.co

Leave a Reply

%d bloggers like this: