How Can I Create A Folder Here? - android

I can't seem to "find" the path to this parent folder on my phone.
The first image is what Windows Explorer says my phone memory looks like.
The second image is what Total Commander says my phone memory looks like.
I have tried many combinations "Internal shared storage", with and without "storage", "0", "emulated", etc.

Try this:
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath() + YOUR_DIR_NAME);
boolean dirOk = directory.mkdirs();
if (dirOk){
// dir created successfully
}else{
//dir create failed
}
In the manifest add:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Related

Android doesn't create folder on my Nexus

I have a Nexus 9 device. I would create a personal folder in the /sdcard/ path, something like this:
/sdcard/MyFolder/
so i coded this:
File directory = new File("/sdcard/MyFolder/");
if(directory.mkdirs()){
Toast.makeText(context, "Folder created", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Folder not created", Toast.LENGTH_SHORT).show();
}
When I launch the app, it shows me "Folder not created" and so it doesn't create folder called MyFolder into /sdcard/ path. In Nexus 9 device the /storage/emulated/0path doesn't exist, so I have to use /sdcard/ path to accessing my storage.
I also used permission in my AndroidManifest.xml file, in this way:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Can you tell me please what's wrong?
Thanks
Since lots of devices have different file structures it is safer to use Environment.getExternalStorageDirectory() for determining the SD card path.
Can you try this code
File folder = new File(Environment.getExternalStorageDirectory() + "/MyFolder");
if (!folder.exists()) {
folder.mkdir();
}
try to specify external storage as listed below:
File directory = new File(Environment.getExternalStorageDirectory().toString()+"/MyFolder");

Unable to read directories or files from a sd card

I am new to android development and I have tried to read a directory from my sd card but didn't got succeeded. Below is the code which I wrote to achieve it
File sdcard = Environment.getExternalStorageDirectory();
if(sdcard.exists()) {
Log.d("Sd card", "Sd card exist");
File[] file_names = sdcard.listFiles();
for(File x : file_names) {
Log.d("File Name",x.getName());
}
}
Control passed the if condition and then I got an NullPointerException at "for each" loop line. Probably function sdcard.listFiles() is returning null. I have an Sd card with many folders and files. Actually I have to create a directory file object for directory "Attachments" which is available on my sd card. I also tried the following code to achieve the same.
File file = new File(android.os.Environment.getExternalStorageDirectory() + File.separator + "Attachments");
and
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Attachments");
on both code when I called the function file.exists(), it returned false.
I have also checked whether sd card is mounted or not by the following code.
String sdCardAvail = Environment.getExternalStorageState();
if(sdCardAvail.equals(Environment.MEDIA_MOUNTED))
Log.d("Card status", "Sd card Available");
The above code has printed "Sd card Available" at log cat.
So please help me out to know whether I am doing it correct or missing any thing.
Thanks....
In order to read or write files on the external storage, your app must acquire the READ_EXTERNAL_STORAGEorWRITE_EXTERNAL_STORAGE system permissions. For example:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
If you need to both read and write files, then you need to request only the WRITE_EXTERNAL_STORAGE permission, because it implicitly requires read access as well.
Also See This Documentation. It Will solve all your future regarding Storage problems.

create visible folder sdcard, Android

I'm developping an application where I need to create new folders/files in the sdcard. The thing is I can see them using a root explorer but not with the default one which comes with Android.
I've taken a look at several similar questions here but don't seem to work for me.
For sure, I'm using in my manifest this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and I'm writing all the Files using:
String path = Environment.getExternalStorageDirectory() + "/FolderName/FileName.format"
but as I said, these new folders/files remained hidden and can only be seen using a root explorer. Neither the folder nor file name starts with "."
Thanks in advance.
Try using mkDirs()
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}

Create folder in sd card

Alright so I created 2 folders in the SD card using the following code:
String folderPath = Environment.getExternalStorageDirectory() + "/AllAroundMe/Images/";
File file = new File(folderPath);
if(!file.exists())
{
if(file.mkdirs());
Log.d("MyTag","Successfully created folders");
}
I tested this program and it really works, the logcat prints the success message above.
But if I navigate to my sd card I don't see "AllAroundMe" folder.
How can I access that folder from my computer?
Try this
Open DDMS perspective -> File Explorer - > mnt -> sdcard
Go to Android DDMS FIleExplorer-->mnt-->sdcard--> and search for your sdcard folder which is created by you
Just check u can`t give the permission in manifest file in your Application just add the permission of this.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Than run your own code the folder has been create in sdcard ok check this.
First i hope you have given the External Storage permission in your Manifest.xml
Do something like this... i know what you did right, but still i prefer this approach.
File f = new File("/sdcard/AllAroundMe/Images/");
Now browse your sdcard from you pc, i am sure you will find the folder.

Can't read the file from internal storage (file doesn't exist issue)

I have a routed device and when I do this
adb shell cat /data/misc/bluetooth/dynamic_auto_pairing.conf
it prints the content of this file.
But in my code when I write something like this, it says that the file does not exist. Well from the console I see it I know is there, but from code I can't read it. My question is what is the problem , am I missing some permission or what is the problem ? can someone provide me with some code to read the content from this file.
Thanks
File pa = new File("/data/misc/bluetooth/","dynamic_auto_pairing.conf");
//this doesn't works also
//File pa = new File("/data/misc/bluetooth","dynamic_auto_pairing.conf");
//File pa = new File("/data/misc/bluetooth/dynamic_auto_pairing.conf");
if(pa.exists()){
Log.v("tag", "does exists");
}else{
Log.v("tag", "does NOT exist");
}
If the file is on sdcard, try:
File pa = new File(Environment.getExternalStorageDirectory() + "/data/misc/bluetooth/dynamic_auto_pairing.conf");
Also try to add:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
outside <application></application> in your manifest file.
EDIT
If the file is in internal memory: Your app can read only from a special folder in internal memory. The path to that folder is returned by:
getFilesDir().getAbsolutePath()
So put the file there and read it with openFileInput().
More info:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
From the docs for File...
public File (String dirPath, String name)
Constructs a new File using the specified directory path and file name, placing a path separator between the two.
In your code you are using...
File pa = new File("/data/misc/bluetooth/","dynamic_auto_pairing.conf");
...and because your dirPath ends with a separator "/data/misc/bluetooth/" it will result in two separators. In other words, effective path will be...
/data/misc/bluetooth//dynamic_auto_pairing.conf
Note the // after 'bluetooth`
If you are using android 6.0 or higher. You must request permission in code.

Categories

Resources