Shahriat Hossain
Shahriat Hossain

Reputation: 73

I am getting java.io.FileNotFoundException when try to capture the screen

I have set all the uses-permission settings outside of the application tag inside my Android manifest file here:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

But when I try to capture the screen I receive the following error:

java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/EMOJI_2-7-116_22538.jpg: open failed: EACCES (Permission denied)

This is method for capture screen.

private void captureScreen() {
        Date now = new Date();
        now.getYear();
        now.getMonth();
        now.getDay();

        View v = findViewById(R.id.rl);
        v.setDrawingCacheEnabled(true);
        Bitmap bitmap = v.getDrawingCache();
        String dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                + File.separator + "Camera" + File.separator + "EMOJI_" +
                now.getDay() + "-" + now.getMonth() + "-" + now.getYear() + "_" + now.getHours() + now.getMinutes() + now.getSeconds() + ".jpg";
        File file = new File(dest);
        try {
            FileOutputStream stream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();
            Toast.makeText(getApplicationContext(), "Saved !", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), "An error occured", Toast.LENGTH_LONG).show();
        } finally {
            v.setDrawingCacheEnabled(false);
        }

        // Scan the image to make it appear in gallery
        MediaScannerConnection.scanFile(this, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null);
    }

Help me how to sort out this problem?

Upvotes: 0

Views: 454

Answers (3)

Hardik Parmar
Hardik Parmar

Reputation: 712

try on this way

File sd = Environment.getExternalStorageDirectory();
File image = new File(sd+filePath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);

May be help full to you.

Upvotes: 0

Positive
Positive

Reputation: 62

You are trying to access a file that is not currently existing on the system.

Instead use..

OutputStream out = new FileoutputStream(dest);

//This time instantiate FileOutputStream Object with String Object not File Object

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007658

If your targetSdkVersion is 23 or higher, you also need to request the WRITE_EXTERNAL_STORAGE permission at runtime.

Upvotes: 1

Related Questions