How to add Write to File Permission on Android

byte[] data;

give imagebyte value to data variable

 // Write to SD Card
FileOutputStream outStream = new FileOutputStream(String.format("/sdcard/piyush.jpg",
            System.currentTimeMillis())); 
        outStream.write(data);
        outStream.close();

Give Write Permission Open AndroidManifest.xml file and put this code

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


This question is quite old but I am going to write this answer anyway since this is the top result when you search for 'write file permission in android'

NotACleverMan's answer is valid but since the release of MarshMallow, users must be asked to provide the permission manually. Therefore, the user must be presented a dialogue where he can explicitly deny or grant the permission for using some functionality.

Taken from the Android developer docs

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen.

So, first you must include this in your manifest file

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

Thereafter, you must request for permission from the user when you need to access the specified resource

ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.WRITE_STORAGE},
            YOUR_PERMISSION_STATIC_CODE_IDENTIFIER);

For more details on the implementation, refer this


Have you got this in your AndroidManifest.xml file?

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

Also, this link has everything you need to know about reading and writing files:

http://www.anddev.org/working_with_files-t115.html

Tags:

Java

Android