ishita
ishita

Reputation: 23

FileNotFoundException open failed: EACCES (Permission denied) on Android Emulator

Following is the code where activity is erroring out : the error happens at line outputStream = new FileOutputStream(file);

public class MainActivity extends AppCompatActivity {

    public void layoutToJPG(View v) {
        View screen = v.getRootView();
// or I've also tried: View screen = new View(this);
        screen.setDrawingCacheEnabled(true);
        Bitmap b = screen.getDrawingCache();
        Log.d("bitmap", b.toString());
        File path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MOVIES);
        File file = new File(path, "/sample.jpg");
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        b.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        Toast.makeText(getApplicationContext(), "Saved to Gallery.", Toast.LENGTH_SHORT).show();
        try {
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Error :

 W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Movies/sample.jpg: open failed: EACCES (Permission denied)
        at libcore.io.IoBridge.open(IoBridge.java:496)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:235)

The above is the error I am getting. I am running it on android emulator Pixel XL API 29 Does it require some special permissions to save file on the emulator.

Upvotes: 2

Views: 1385

Answers (2)

user13146129
user13146129

Reputation:

You have to add permission. And, ask user to accept permission.

Manifest :

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

Request permission :

There's a library which you can use to ask for permission

Dexter.withContext(this)
.withPermission(Manifest.permission.CAMERA)
.withListener(new PermissionListener() {
    @Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */}
    @Override public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}
    @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}).check();

Over than API level 23(maybe, I actually forgot) you have to request user to grant permission. That's why you have to add above source code.

Upvotes: 1

ahmad bajwa
ahmad bajwa

Reputation: 1068

As.

Android's permission system is one of the biggest security concern all along since those permissions are asked for at install time. Once installed, the application will be able to access all of things granted without any user's acknowledgement what exactly application does with the permission.

Android 6.0 Marshmallow introduces one of the largest changes to the permissions model with the addition of runtime permissions, a new permission model that replaces the existing install time permissions model when you target API 23 and the app is running on an Android 6.0+ device

Try this code.

if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        1);
            }
        }

Add this in Menifest:

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

Upvotes: 1

Related Questions