how to create a folder internal storage in android lollipop - android

i have tried most of the code i have found on stack over flow read the devlopment documentation but i still fail to create a folder and a file on internal storage in android lollipop i have not tried on lower api but i even tried those internal persmission declaration in manifest.
the below sample of the code i tried do not even work for my situation:
String path = Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/appFolder";
File mFolder = new File(path);
if (!mFolder.exists()) {
mFolder.mkdir();
}
File Directory = new File("/sdcard/myappFolder/");
Directory.mkdirs();
i tried this also below:
File myDir = context.getFilesDir();
// Documents Path
String documents = "documents/data";
File documentsFolder = new File(myDir, documents);
documentsFolder.mkdirs();
String publicC = "documents/public/api.txt" ;
File publicFolder = new File(myDir, publicC);
publicFolder.mkdirs();
and then this as well:
ContextWrapper contextWrapper = new ContextWrapper(
getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);

First, I recommend you read more on what the following terms mean with respect to Android app development:
internal storage
external storage
removable storage
With that as background, let's review what you did:
Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/appFolder";
Never use getDataDirectory(). I have no idea where this code would point to.
File Directory = new File("/sdcard/myappFolder/");
You do not have arbitrary read/write access to removable storage, and removable storage may not be found at that location anyway.
File myDir = context.getFilesDir();
// Documents Path
String documents = "documents/data";
File documentsFolder = new File(myDir, documents);
This code is fine. However, it points to internal storage, and on Android devices, you cannot see internal storage very readily. That too is fine, as developers should be used to the idea that they cannot see everything that their code affects. You might consider writing test cases to confirm that your directory was created.
String publicC = "documents/public/api.txt" ;
File publicFolder = new File(myDir, publicC);
This points to nowhere. Always use some method to derive the base path.
ContextWrapper contextWrapper = new ContextWrapper(
getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);
The ContextWrapper is useless. filepath needs to be a simple directory name. You could simplify this as:
File directory = getDir(directoryName, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);
Then this code is also fine. It too points to internal storage, and therefore you will not be able to examine it directly. Once again, write test code to confirm that the directory was created as you expect.

Related

Directory not creating in internal storage

I am trying to create a directory in the internal storage into an Android device using the following code but it seems like I am missing something. The directory is not showing in the internal storage. I have tried this code:
File testDir = DirectoryTestActivity.this.getDir("Test", Context.MODE_PRIVATE);
if (!testDir.exists())
{
testDir.mkdirs();
}
add you internal path in new File(your internal path )
File newFolderVideos = new File(context.getFilesDir().getAbsolutePath() + "/Pictures");
if (!newFolderVideos .exists()) {
newFolderVideos.mkdirs();
}

How can we create a folder of app in device storage in Android?

I'm using code in comment to save a file in directory folder. This code is working properly with mobile phone having sd card.
But if I use it with mobile phone not having sd card it is giving IO exception.
How can I create a folder like whatsapp has in device storage root to save images etc but I want a folder of application name to save .csv files in my device storage root.
First make sure you have added Storage Permissions inside Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
If you want to create folder inside ExternalDirectory
File f = new File(Environment.getExternalStorageDirectory()+"/foldername");
if(!f.exists()) f.mkdir();
If you want to create file inside internal storage
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal directory;
File fileWithinMyDir = new File(mydir, "myfile");
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
You can create directory in internal storage and external storage as Follows.
//check whether Sdcard present or not
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
// yes SD-card is present
String directory = "/your Directory name or app name ";
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + directory;
File dir = new File(path);
if(!dir.exists()) //check if not created then create the firectory
dir.mkdirs();
//add your files in directory as follows
File file = new File(dir, filename);
}
else
{
// create folder in internal memory
File mydir = context.getDir(directory, Context.MODE_PRIVATE); //Creating an internal directry
if(!mydir.exists()) //check if not created then create the firectory
mydir.mkdirs();
//add your files in directory as follows
File file = new File(mydir, filename);
I hope it will work for you

Get Internal Storage custom directory location

I' am trying to get the location of my Internal Storage dynamically but having issues with it. The following is my code that am currently working with:
Context context = this;
File dir = context.getDir("appdata", Context.MODE_PRIVATE);
File file = new File(dir, "name.txt");
System.out.println( file.toString() );
The return path from the print is /data/data/com.example.application.form/app/name.txt but I want Internal Storage/appdata/name.txt
What am I doing wrong?
use
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
instead
File dir = context.getDir("appdata", Context.MODE_PRIVATE);

Android application file path

So I've built some code to download a file which works fine and I have set it to download into the applications directory which works. it's stored in the application folder /files/dltest
My issue is with checking programatically wether or not the file exists, I've tried methods one stackoverflow and for some reason I can only get my hard coded path to work.
/sdcard/Android/Data/com.test.alihassan.download/files/dltest/REQS.pdf
Using built in methods to retrieve the path gives me the same path but with /data/data/com.... and this doesn't work
File mydir = context.getFilesDir();
File fileWithinMyDir = new File(mydir, "myfile/path/fileNmae");
if(fileWithinMyDir.exists()){
//exists
}else{
//not exists
}
Update:
//File mydir = this.getFilesDir();
File mydir = this.getExternalFilesDir("/dltest/REQS.pdf");
if (mydir.exists()) {
//exists
} else {
//not exists
}

Copy database when phone hasn't got external memory

I have got a problem with backup database in my app. I am working on Android 2.2.3 and this has sd card installed. Making copy of the database works fine. The problem occures when I'am testing my app on the phone with internal memory enought big like sd cards (Nexus 32gb). In this scenario my method doesn't work extracting file to sd card because it doesn't (sd card) exist. How to make copy of database to internal independed location? I've tried:
File outPut = new File(Environment.getRootDirectory().toString() + "/myfolder/");
but got permission denied and can not create folder with data. Can anyone show correct way?
EDITED:
I don't get it. I'd like to make new folder with dataBackup. I've defined correct location for that but it says that can not find file. SDCard is present. Why it can not create that folder - "/storeUGif/databaseName.db".?
Here is absolute path for destination folder:
public static final String OUTPUT_BACKUP_DATABASE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/storeUGif/" + SqliteHelper.DATABASE_NAME;
if(isSdPresent())
{
//File outPut = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/StoreUGif/"+SqliteHelper.DATABASE_NAME);
File outPut = new File(Tools.OUTPUT_BACKUP_DATABASE);
File storeUGif = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString());
storeUGif.mkdir();
File currentDB = getApplicationContext().getDatabasePath(SqliteHelper.DATABASE_NAME);
FileInputStream is = new FileInputStream(currentDB);
OutputStream os = new FileOutputStream(outPut);
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}
os.flush();
os.close();
is.close();
}
else
{
Toast.makeText(this, "SDCard is unvailable", Toast.LENGTH_SHORT).show();
}
Use Environment.getExternalStorageDirectory() to get a valid path.
Despite its name, it will return the default storage, either the external or (if missiing) the internal one.
For reference: http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()

Categories

Resources