Accessing SD CARD and doing FILE IO Operations - android

I am trying to create a folder and then after that do some file IO operations!
I am using a sony Xperia Z to test this out!
I know right now I've hardcoded the location but it doesn't let me create folders!
File appPath = new File("/storage/sdcard1/folder");
if (!appPath.exists()) {
appPath.mkdirs();
}
I am using a targetSdkVersion of 22
And having lollipop on my phone.
I tried
appPath.mkDir();
as well but all this gives a value of False.
And i have added permissions to manifest
<uses-permission name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission name="android.permission.READ_EXTERNAL_STORAGE" />
And I tried many different open source file manager but none are able to create folders, But ES file Manager is able to create folders and do File IO operations!

Don't hardcode global paths like /sdcard, use Environment.getExternalStorageDirectory() and related methods instead. Here is a working sample.
public static String getNewFolderPath() {
File folder = new File(Environment.getExternalStorageDirectory()
.toString() + File.separator + "folder");
if (!folder.exists())
folder.mkdirs();
return folder.getAbsolutePath();
}
EDIT:
For more info check:
Find an external SD card location
#CommonsWare Answer
#Aleadam Answer
Hope it helps!

Related

How to create folder in root of storage Android

I'm new at this.
Telegram, Whatsapp, Vk, Aliexpress - All this apps can write they folders in /Storage/emulated/0
How can I create my folder in this place for Android 5...10?
When I try to use File("/storage/emulated/0/", myfile) - it doesn't work.
Please, can anyone give some mini example how can I create my files in storage
Since you haven't written any code, I can't tell you what you're doing wrong. But this is something that might help you.
First you need to declare permissions on your manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
After that you can simply use this to create your folder.
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
"folderName";
File directory = new File(path);
directory.mkdirs(); // It returns a boolean you can use it to check if your folder
// was created.
Yes, You can create Folders inside the External Storage too.
But, keep in mind that you cannot use emulated/0 because it is different for different devices.
File path = new
File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Folder/");
if(!path.exists()){
boolean k = path.mkdirs();
}
Although, Environment.getExternalStorageDirectory() is deprecated as of API29. So You can use getExternalDir() too.

Can't create new file path in External Storage

I am new to Android and mostly using snippets of code from other posts to build my project. I am having a hard time creating a new directory and file on my device. I am using the following code, but I am unable to verify the success of the creation of this path. I want to be able to mount my phone to my laptop and find a file named "MyRecording.pcm" in a folder "/My/Files". I am using the boolean value of mkdirs() to verify whether or not the path was created on my device. If that path was not created then my TextView will tell me "Directories do not exist"; otherwise, my code will create the file MyRecording.pcm. I keep getting an error/warning "result of mkdirs() is ignored". Please help.
File path = new File(Environment.getExternalStorageDirectory() + "/My/Files");
path.mkdirs();
if(!path.exists()) { statusText.setText("Directories do not exist");}
else recordingFile = File.createTempFile("MyRecording", ".pcm", path);
Do you have the permission set in your manifest?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Also, Android Studio is giving you the warning about mkDirs () because it returns a boolean indicating whether the directory was created. It's just reminding you that you never used the result. It doesn't matter.

Can't get sdcard real path on Android 6.0

My android phone is Sony Z3, and the version is Android 6.0.1, when I use
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) to get the SDCard DCIM path, which returns path is /storage/emulated/0/DCIM,
but my sdcard real path is /storage/3FC5-160A/DCIM. Any one know how to get the correct sdcard path on Android 6? Thanks.
Also, I have add the external storage read and write permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I had to do this specifically for M. Get all the dirs with context.getExternalCacheDirs() then check if it's removable
// For Marshmallow, use getExternalCacheDirs() instead of System.getenv("SECONDARY_STORAGE")
// to determine if there are mounted SD cards
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
File[] externalCacheDirs = context.getExternalCacheDirs();
for (File file : externalCacheDirs) {
if (Environment.isExternalStorageRemovable(file)) {
// Path is in format /storage.../Android....
// Get everything before /Android
externalSdCardPath = file.getPath().split("/Android")[0];
break;
}
}
After a lot of research I found a solution. Code at below gives you the path in "SD card/Android/data/your app location " then you can do whatever you want.
getApplicationContext().getExternalCacheDirs()[1]
use getExternalStorageDirectory() instead of getExternalStoragePublicDirectory() like below
Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/";
or check path first
final String path = android.os.Environment.DIRECTORY_DCIM;
I'm using the access storage framework. I get it by handling the string of uri and get the "3FC5-160A" part and then combine it to the sdcard path.
You can check my detailed answer here
https://stackoverflow.com/a/38648200/6530106

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();
}

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