How to Take Screenshot in Android App and Open Image File Programmatically in Android Studio and Java

What we are going to look at today is How to Take Screenshot in Android App and Open Image File Programmatically in Android Studio and Java We are going to be very clear about this topic. We hope that all the skepticism on this topic will be resolved through this article.

How to Take Screenshot in Android App and Open Image File Programmatically in Android Studio and Java

Take Screenshot in Android App and Open Image File Programmatically in Android Studio and Java

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”
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>

Read Also: How to Adjust the Volume in Android Phone Programmatically in App Using Android Studio

Final Words

We hope you find the how-to Take Screenshot in Android App and Open Image File Programmatically in Android Studio and Java useful. And please let us know if you have any doubts about this article.

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment