Error trying to save screenshot

1

I'm trying to save a screenshot when I click on a button, but it makes an error when I press that button. the code I use to take the photo and save it.

  public Bitmap takeScreenshot() {
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

and this is the code when you press the button

 public void salir (View view){
    Bitmap bitmap = takeScreenshot();
    saveBitmap(bitmap);
}

This is the error message

E/GREC: /storage/emulated/0/screenshot.png: open failed: EACCES (Permission denied)
    java.io.FileNotFoundException: /storage/emulated/0/screenshot.png: open failed: EACCES (Permission denied)
        at libcore.io.IoBridge.open(IoBridge.java:452)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
        at com.example.oscar.app.consulta.saveBitmap(consulta.java:72)
        at com.example.oscar.app.consulta.salir(consulta.java:86)
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
        at libcore.io.Posix.open(Native Method)
        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
        at libcore.io.IoBridge.open(IoBridge.java:438)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:87) 
        at java.io.FileOutputStream.<init>(FileOutputStream.java:72) 
        at com.example.oscar.app.consulta.saveBitmap(consulta.java:72) 
        at com.example.oscar.app.consulta.salir(consulta.java:86) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
        at android.view.View.performClick(View.java:5198) 
        at android.view.View$PerformClick.run(View.java:21147) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    
asked by oscar ramirez 03.12.2016 в 15:23
source

2 answers

-1

Add permission on the AndroidManifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

EDITING

As you are using Android 6.0, the models or rather the flow of permissions in these versions or porteriores changed. Now it is the user who decides to give the permissions once the application executes. Formerly when the user installed the app warned and showed what permissions had to give permissions to execute it, now, today with these versions once the user runs the application And it is necessary to use such permissions the application should ask (you are the one you must control this by code). What you must do is first verify the version of the android where the app is running and if it is a later or equal to 6 verify those permissions. I leave a post where they explain much better and you can solve it. In this post you will see this or other permissions that you need to access in your app.

Android Permissions

    
answered by 03.12.2016 / 15:40
source
-1

This is related to Android 6.0 and it is not enough to add this permission in AndroidManifest.xml :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

I propose this method so that you call it when you start your application and in this way you configure the permission:

private void checkWritePermission() {
    int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para enviar escribir.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso para enviar Escribir!");
    }
}
    
answered by 04.01.2017 в 21:02