How to create your own snipping tool Android App to take part screenshot using onTouchEvent?

In this video it shows how one can capture the touch points of the user on the phone screen and use it to take snippet of the screen as image. For screenshot it uses part of the code shown in the below page:
https://programmerworld.co/android/how-to-take-screenshot-from-your-android-11-app/

However, it modifies the code to show how only a part of the screenshot can be taken making it a custom snipping tool Android App.

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Complete source code:

package com.programmerworld.snippingtoolforscreenshot;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {

private TextView textView;
private float floatStart_X=-1, floatStart_Y=-1, floatEnd_X=-1, floatEnd_Y=-1;

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

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE},
PackageManager.PERMISSION_GRANTED);

textView = findViewById(R.id.textView);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
floatStart_X = event.getX();
floatStart_Y = event.getY();
}
if (event.getAction() == MotionEvent.ACTION_UP){
floatEnd_X = event.getX();
floatEnd_Y = event.getY();
}
return super.onTouchEvent(event);
}

public void buttonScreenshot(View view){
if (floatStart_X == -1 ||
floatStart_Y == -1 ||
floatEnd_X == -1 ||
floatEnd_Y == -1){
textView.setText("Dimensions not captured. \n Try Again.");
return;
}

textView.setText("Starting ... ");
View view1 = view.getRootView();
// View view1 = getWindow().getDecorView().getRootView();

Bitmap bitmap = Bitmap.createBitmap(view1.getWidth(), view1.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view1.draw(canvas);

Bitmap bitmapSnippet = Bitmap.createBitmap(bitmap,
Math.round(floatStart_X),
Math.round(floatStart_Y),
Math.round(floatEnd_X-floatStart_X),
Math.round(floatEnd_Y-floatStart_Y));

File fileScreenshot = new File(this.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
Calendar.getInstance().getTime().toString()+".jpg");

try {
FileOutputStream fileOutputStream = new FileOutputStream(fileScreenshot);
bitmapSnippet.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
textView.setText("SUCCESS");
}
catch (Exception e){
textView.setText("FAILURE");
}
}
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.programmerworld.snippingtoolforscreenshot">


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_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/Theme.SnippingToolForScreenshot">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="216dp"
android:layout_height="90dp"
android:text="Programmer World!"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="195dp"
android:layout_height="65dp"
android:layout_marginStart="98dp"
android:layout_marginTop="113dp"
android:onClick="buttonScreenshot"
android:text="Snipping capture"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

plugins {
id 'com.android.application'
}

android {
compileSdk 31

defaultConfig {
applicationId "com.programmerworld.snippingtoolforscreenshot"
minSdk 31
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Leave a Reply

%d bloggers like this:
Available for Amazon Prime