-1

I want to save an bitmap as screenshot into an /fraktal folder. Every time i try this i get an Error saying EACCESS permission denied at file.createNewFile(). I already added the permissions at AndroidManifest.xml.

uses-permission android:name="android.permission.INTERNET"

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 

uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"

May u help?

private void takeScreenshot(Bitmap fractal)
{
    String filename="fraktal_"+ System.currentTimeMillis();
    FileOutputStream out = null;
    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES).getAbsoluteFile();
    File file = new File(path,"/fraktal/"+filename +".png");

    try {
        path.mkdirs();

        file.createNewFile();

        out = new FileOutputStream(file);
        fractal.compress(Bitmap.CompressFormat.PNG, 100, out);// PNG ist         verlustfrei, kompression 100 wird ignoriert
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null)
            {
                out.flush();
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
Vishal Senjaliya
  • 454
  • 6
  • 21

1 Answers1

0

Runtime permision check try this

public  boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        return true;
    } else {

        Log.v(TAG,"Permission is revoked");
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        return false;
    }
}
else { //permission is automatically granted on sdk<23 upon installation
    Log.v(TAG,"Permission is granted");
    return true;
}
}
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31