How to take Screenshot Programmatically in Android and share
When I try to take a Screenshot and save it on the Download folder. It's not working for me and it gives me an error. I am a newbie to android app development. So, I don't know what to do for saving and sharing that photo to WhatsApp?
This is my button:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to take a Screenshot"
android:onClick="ScreenshotButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
In my Android Manifest File (Getting permission from the user):
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
This is my Java code to take a screenshot and save on Phone's Download folder:
ActivityCompat.requestPermissions(this, new String[]{WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED); //To check if user permitted or not
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();
}
}
I am getting this error every time when I press my button:
I/.screenshottes: NativeAlloc concurrent copying GC freed 1732(166KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 1640KB/3281KB, paused 4.216ms total 245.239ms
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Downloads/Sat Jul 24 12:10:05 GMT+05:30 2021.jpg: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:496)
at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
at java.io.FileOutputStream.<init>(FileOutputStream.java:186)
at com.gprprinters.screenshottest.MainActivity.ScreenshotButton(MainActivity.java:44)
at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:7125)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
W/System.err: at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7255)
at libcore.io.IoBridge.open(IoBridge.java:482)
... 17 more
I/.screenshottes: NativeAlloc concurrent copying GC freed 572(57KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 1681KB/3363KB, paused 56.199ms total 227.094ms
D/EGL_emulation: eglMakeCurrent: 0xdd183500: ver 2 0 (tinfo 0xdd1b3f00)
If you know. Plz, help me
Thanks in Advance!