Skip to content Skip to sidebar Skip to footer

Application Does Not Write File To Internal Storage [android]

I am trying to create and then write a simple file called 'thisisafile.txt' in Android. After I run the application, file does not exist in internal storage. import java.io.*; im

Solution 1:

For Read

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

For Write

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

Need Read / Write Permission On Manifest, Allow Storage Permission.

privatevoidwriteToFile(String data,Context context) {
    try {
        OutputStreamWriter outputStreamWriter = newOutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    } 
}

Solution 2:

Referrence:

https://developer.android.com/training/permissions/requesting#javahttps://stackoverflow.com/a/44155064/5207684

Use the following path for a file. It will write file to your root folder of storage.

StringextStorageDirectory= Environment.getExternalStorageDirectory().toString();
File file= newFile(extStorageDirectory, "config.txt");

writeToFile("File content".getBytes(), file);

writeToFile

publicstaticvoidwriteToFile(byte[] data, File file)throws IOException {

    BufferedOutputStreambos=null;

    try {
        FileOutputStreamfos=newFileOutputStream(file);
        bos = newBufferedOutputStream(fos);
        bos.write(data);
    }
    finally {
        if (bos != null) {
            try {
                bos.flush ();
                bos.close ();
            }
            catch (Exception e) {
            }
        }
    }
}

add external storage writing AND reading permissions in the manifest:

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

After Android 6.0 WRITE_EXTERNAL_STORAGE permission must request in run time

requestPermissions(newString[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE}, 1);

and then can handle the result of permission request in onRequestPermissionsResult() method inside activity called from it.

Example:

// Here, thisActivity is the current activityif (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted// Should we show an explanation?if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {
        // Show an explanation to the user *asynchronously* -- don't block// this thread waiting for the user's response! After the user// sees the explanation, try again to request the permission.
    } else {
        // No explanation needed; request the permissionActivityCompat.requestPermissions(thisActivity,
                newString[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an// app-defined int constant. The callback method gets the// result of the request.
    }
} else {
    // Permission has already been granted
}

Check on real device instead of Emulator.

Post a Comment for "Application Does Not Write File To Internal Storage [android]"