Save Internal File in My Own Internal Folder in Android

To save file in a directory of Internal Storage

Try this it might help you. For the above marshmallow version please check the write permissions.

public void saveToExternalStorage() {
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/directoryName";
try
{
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream fOut = null;
File file = new File(fullPath, "fileName.txt");
if(file.exists())
file.delete();
file.createNewFile();
fOut = new FileOutputStream(file);
fOut.flush();
fOut.close();
}
catch (Exception e)
{
Log.e("saveToExternalStorage()", e.getMessage());
}

}

Write a file in internal storage specific folder in Android

you can go this way.

File directory = new File("path_to_directory");
try {
if(!file.exists()) {
directory.createNewFile();
}
File dataFile = new File(directory, "Your File Name");
FileOutputStream stream = new FileOutputStream(dataFile, true); // true if append is required.
stream.write();
stream.flush()
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (null != stream) {
stream.close();
}

Here path_to_directory = Environment.getExternalStorageDirectory() + File.seperator + "FolderName";

OR

path_to_directory = ctx.getFilesDirectory() + File.seperator + "FolderName";

By the way, you cannot create a folder into android's internal storage until it is rooted. It will definitely give you the IOException because without root the android internal FS is read-only. So be aware of that.

Thanks,
Happy Coding :-)

Saving Files in Android - For Beginners (Internal / External Storage)

The terms "Internal Storage" and "External Storage" might be confusing at first, because Google's intentions are different from what we would expect & know from our day-to-day use of language: "External" doesn't necessarily mean the "SD Card". This guy made a great article about the terminology confusion

According to your intentions, you'd want to be working with the External Storage concept. The differences are well explained in the Documentation, but I'll shortly brief them to you here.

At the end I'll provide you an example, but first lets know the basics:

Internal Storage

  • Files are accessible by only your app
  • Files are removed when your app is uninstalled
  • Files are always available (meaning they files will never be saved on a removable memory)

External Storage

  • Files are fully readable by other apps (including any variant of File Manager app, in your case)
  • Files aren't necessarily removed when your app is uninstalled - explained later
  • Files availability isn't guaranteed (can be deleted by other apps / removable memory)

So now that we know you need External Storage, there are several things needed to be done before starting:

  • Require Permissions (read/write) inside your Manifest.xml file, depending on your needs:
    <manifest ...>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>

Each permission stands by its own, meaning you don't need to have both if, for example, you only wish to read files instead of writing them

  • Verify that storage is available - this is done on runtime, and is well explained inside the documentation. We need to make sure the storage is mounted into the device / its state is not problematic somehow in a way that would cause a failure of read/write requests.

Example Time!

In the given method, we will save a text file inside the root directory.

Credits to this article

public void writeFileExternalStorage() {

//Text of the Document
String textToWrite = "bla bla bla";

//Checking the availability state of the External Storage.
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {

//If it isn't mounted - we can't write into it.
return;
}

//Create a new file that points to the root directory, with the given name:
File file = new File(getExternalFilesDir(null), filenameExternal);

//This point and below is responsible for the write operation
FileOutputStream outputStream = null;
try {
file.createNewFile();
//second argument of FileOutputStream constructor indicates whether
//to append or create new file if one exists
outputStream = new FileOutputStream(file, true);

outputStream.write(textToWrite.getBytes());
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}

I'd like to answer specifically to some of your questions:

do I need to create those folders in the android studio as adding folders? or do I need to create it from code?

Definitely not via the Android Studio. These are your projects folder, containing your code. The way to do it is mentioned above.

I couldn't find the folder with my app name and inside it my Databases and Images folders... What am I doing wrong?

Probably saved your files as Internal Storage ones / saved them as project folders as you mentioned earlier - and those wouldn't (and shouldn't) show up.


Useful things to know

There are 2 types of directories: public and private.

Private

  • Not accessible by the Media Store
  • Files are removed when app is uninstalled
  • Retrieved by getExternalFilesDir(...) method

Example: the WhatsApp directory (in my phone) is located right at the root level. Calling it would be: getExternalFilesDir("WhatsApp/...")

Public (Downloads/Movies/Images libraries)

  • Files are scanned by the MediaStore
  • Retrieved by Environment.getExternalStoragePublicDirectory(...) method

Example: getting the Documents folder would look like: Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)

Writing to file in Documents folder in internal storage

File file = new File(Environment.DIRECTORY_DOCUMENTS, "myFile.txt");

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "myFile.txt");

Unable to save file in internal directory

I a trying to create folder in internal storage in android

FWIW, your code is set up for external storage.

Someone please let me know what I am doing wrong any help would be appreciated.

Papers/ appears to exist and is a directory. writeTo() takes a file, not a directory. Try something like:

        val mediaStorageDir = File(Environment.getExternalStorageDirectory(), "Papers")

if (mediaStorageDir.exists()) {
val pdfFile = File(mediaStorageDir, "something.pdf")

document.writeTo( FileOutputStream(pdfFile))
document.close()
}

android - save file to internal storage

You can try with below:

ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(getFilesDir().getName(), Context.MODE_PRIVATE);
File file = new File(directory,”fileName”);
String data = “TEST DATA”;
FileOutputStream fos = new FileOutputStream(“fileName”, true); // save
fos.write(data.getBytes());
fos.close();

This will write the file in to the Device's internal storage at /data/user/0/com.yourapp/



Related Topics



Leave a reply



Submit