Creating a folder with file in Android's internal storage - android

i'm trying to create a folder with a .csv file inside in android internal storage, but the folder doesn't appear in the regular file explorers.
The code is the following.
private String FOLDER_NAME = "app_folder";
File folder = new File(Environment.getDataDirectory().getAbsolutePath() + File.separator + FOLDER_NAME);
if( !folder.exists() )
folder.mkdir();

but the folder doesn't appear in the regular file explorers.
That is because internal storage cannot be viewed by any sort of file explorer, except on emulators or rooted devices.
File explorers — on-device and desktop — usually examine external storage.

Related

Is there any functionality which while installing the android app asks the user about the location where to install the obb file?

I have a resources folder amounting 2-3 GBs. I want to install the app with the obb in SD card directly as some users won't be having that much internal storage.
I am currently searching for the solution.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="preferExternal"
... >
I found out about this which installs the apk on external storage, but not the resources.
I am expecting that by default there must be an option in which apk is installed in the internal storage while the resources on the sd-card.
Well you first need the path the user wants the resources to be downloaded in.
Use AsyncTask to download zip file of the resource files
(You can also check this thread here for more details)
Unzip the files to the select path and then delete the the zip file
if you want the resources to be installed by default in the external sd card first check if they have one for it
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
//Check for the file
File appFolder = new File(Environment.getExternalStorageDirectory() + File.separator
+ context.getString(R.string.app_name));
boolean exist = appFolder.exists();
}
then just set the path to it
if (exist == true){//your code here for the path}
though it is not wise as an external storage does not mean that it is a SD card

How to create directory programatically in your app project?

I want to create directory in my android project. I have tried below code I could not find any directory in my project.
File myDir = new File(getCacheDir(), "files");
myDir.mkdir();
if(!myDir.exists())
myDir.mkdirs();
If you just want to create a Dir in your internal storage you can do it in the below method
File mydir = context.getDir("files", Context.MODE_PRIVATE); //Creating an internal dir;
if (!mydir.exists())
{
mydir.mkdirs();
}
getFilesDir() returns a File object to a directory that is private to your application only.
getDir() enables you to create any file or directory in the internal memory, which is also accessible by other applications depending on the mode you create it.
getCacheDir() returns the absolute path to the application specific cache directory on the filesystem but doesn't enable you to create Dir there. You can create a file though.
Please note that this file will be stored in internal memory and can be cleared by system as well as user manually on clearing cache.

Deleting file from internal storage

How do I delete a file or folder on the internal storage in android with Delphi for android
You should use the TFile and TDirectory classes in the System.IOUtils unit.
For example:
TDirectory.Delete(<YOUR DIR PATH>);
or
TFile.Delete(<YOUR FILE PATH>);
Look in Embarcadero's documentation to get the right path of your files and folders on the various platforms:
Standard RTL Path Functions across the Supported Target Platforms
Referred from :
https://stackoverflow.com/a/27047502/5193608
https://stackoverflow.com/a/3554949/5193608
Main Part from both links:
You should always delete files that you no longer need. The most straightforward way to delete a file is to have the opened file reference call delete() on itself.
myFile.delete();
If the file is saved on internal storage, you can also ask the Context to locate and delete a file by calling deleteFile():
myContext.deleteFile(fileName);
Note: When the user uninstalls your app, the Android system deletes the following: All files you saved on internal storage All files you saved on external storage using getExternalFilesDir(). However, you should manually delete all cached files created with getCacheDir() on a regular basis and also regularly delete other files you no longer need.
Source : http://developer.android.com/training/basics/data-storage/files.html
Directly you can do is :
File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();
Hope,it helps!

Android: File Location

I am writing to a path mnt/sdcard/foldername. I get no errors and the file is written, but when I browse the internal storage of my phone I can not find the folder. Where would the folder be located? I have a galaxy nexus?
Code is here:
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/statReporter/");
directory.mkdirs();
Log.d("Tag", directory.toString());
//Path and name of XML file
File file = new File(directory, appendTimeStamp("phoneNum") + ".csv");
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//Write to CSV File
osw.write(message);
osw.write(',');
//Flush and close OutPutStreamWriter and close FileOutputStream
osw.flush();
osw.close();
fOut.close();
Log.d("File Logger:", "File write successfully!");
Cant find it in Windows Computer\Galaxy Nexus\Internal Storage but all other folders appear.
I used an app called OI File Managaer and I can view the folder and file on phone but how do I view it through Windows OS?
If you really want to find files on your device, I'd recommend one of the Google Play apps you can find (I personally like ASTRO File Manager) or, from the PC you can use (for instance) Android Commander (which, incidentally, will let you use the same file path structure you'll be using from within your developing environment).
I believe that Android devices will not actually show you all paths and available files when you browse it, for instance, with Windows Explorer.
If you're using Eclipse, in the DDMS perspective you can browse your device's file structure. On your right you have the "File Explorer" and as far as I remember, it's a pretty complete tool.
Don't use /mnt/sdcard for accessing external storage. You can use Environment.getExternalStorageDirectory() to access path to external storage. This may vary from device to device.

How to write on ExternalStorage of Emulator

I am trying to write folders (make folders) on mnt/sdcard/ but directory is not making on emulator external strorage.
Is there any way to create folders and files on Emulator's External Storage
Code:
File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"myNewDir");
success =file.mkdir();
if(!success)
//Not Created
esle
//Created

Categories

Resources