Below is my MainActivity and Manifest file. I am trying to implement a screenshot in my app which then gives the option to open the screenshot using other applications. When I click the button in the main activity nothing is happening. It keeps throwing java.io.FileNotFoundException like so
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/ScreenShooter/2021-02-19_07:45:59.jpg: open failed: ENOENT (No such file or directory)
Is there a way to solve this?
package com.levirs.example.screenshooter;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
screenshoot();
}
});
}
private void screenshoot() {
Date date = new Date();
CharSequence now = android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", date);
String filename = Environment.getExternalStorageDirectory() + "/ScreenShooter/" + now + ".jpg";
View root = getWindow().getDecorView();
root.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(root.getDrawingCache());
root.setDrawingCacheEnabled(false);
File file = new File(filename);
file.getParentFile().mkdirs();
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.levirs.example.screenshooter">
<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 more here: https://stackoverflow.com/questions/66276593/filenotfoundexception-when-trying-to-screenshot
Content Attribution
This content was originally published by b0bbybtch at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.