How to create a file in an SDCARD directory - android

I want to create a file(not created) in a directory(not created) in the SDCARD.
How doing it ?
Thank you.

Try the following example:
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//handle case of no SDCARD present
} else {
String dir = Environment.getExternalStorageDirectory()+File.separator+"myDirectory";
//create folder
File folder = new File(dir); //folder name
folder.mkdirs();
//create file
File file = new File(dir, "filename.extension");
}
Don't forget to add the permission to your AndroidManifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The problem is that mkdirs() is called on a File object containing the whole path up to the actual file. It should be called on a File object containing the Path (the directory) and only that. Then you should use another File object to create the actual file.

You should also have to add permission to write to external media.
Add following line in the application manifest file, somewhere between <manifest> tags, but not inside <application> tag:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Use This:
DocumentFile tempDir = DocumentFile.fromSingleUri(context,targetDirectoryUri);
DocumentFile newDocumentFile = tempDir.createFile(type,name);
"targetDirectoryUri" is uri of the directory you want to put the file into it.
This is the only solution!
After Api 19 you can not write on SDCard, so you must use DocumentFile
instead File.
In addition, you must also take SDCard permission. To learn how to do this and get the targetDirectoryUri, please read this.

You can use in Kotlin
val file = File( this.getExternalFilesDir(null)?.getAbsolutePath(),"/your_image_path")
if (!file.exists()) {
file.mkdir()
}
Don't forget to give permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

See here why creating files on root of the SD card is a bad idea

Related

How to make a folder on external SDCARD (Which is inserted from out side the device)?

I need to create a folder on my external storage (inserted SD card). I tried in several ways, but it just create a folder on device storage. I have seen to create on some android app. Please help me by providing a solution. Thanks in advance.
File directory = new File(Environment.getExternalStorageDirectory() + "/boyan/");
if (!directory.exists()) {
directory.mkdirs();
}
Permissions are:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Do it like this
File file = new File(Environment.getExternalStorageDirectory(), "Folder_name");
if (!file.exists()) {
file.mkdirs();
}
I got the solution.
Here it is.
Environment.getExternalStorageDirectory()
Above code return "/storage/sdcard0", But if you want to make a folder on external SD Card you need "/storage/sdcard1". so try to do something like this
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath().replace("0", "1") + "/boyan/");

Why can i not make a directory inside Environment.DIRECTORY_PICTURES?

This is my code
File selfieLocation = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"Daily Selfies");
boolean isDirectory = false;
if(!selfieLocation.isDirectory()) {
//Creates directory named by this file
selfieLocation.mkdir();
isDirectory = selfieLocation.isDirectory();
}
//array of strings
for(String selfiePath: selfieLocation.list()) {
selfies.add(selfiePath);
}
Basically what I am trying to do is create my own customizable directory inside of the standard directory in which to place pictures that are available to the user.
I looked at related threads and saw this one, Android: unable to create a directory in default pictures folder. However I made sure that I had a call to getExternal...., and not just have Environment.DIRECTORY_PICTURES as a parameter.
I also looked on here http://developer.android.com/guide/topics/data/data-storage.html#filesExternal and saw that I had the right method call/format to create a customizable folder in external memory. The docs example was
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
I stepped through my code and saw that the local variable isDirectory stayed at false even after the call to selfieLocation.mkdir(). Does anyone know why this directory cannot be created?
Try to create directory with File#mkdirs(), not File#mkdir(). The latter assumes that all parent directories are already in place, so it won't create directory if any of its parents don't exist.
Also, take a look at your permissions in AndroidManifest.xml. You need the following permissions in order to read/write the content on external storage:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
</manifest>
android.permission.READ_EXTERNAL_STORAGE isn't required for now, but it will be in the future releases of Android.

how to create folder with my application name on Android Root

how to create folder with my application name on android internal memory not on the SDcard and create on it files like whatsapp and viber any ideas ?
Thanks
File storagePath = new File(Environment.getExternalStorageDirectory()+ "/foldername");
storagePath.mkdirs();
don't forget to add permission in manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You can use for example:
private MainActivity activity;
[...]
File fileDir = this.activity.getFilesDir();
I used this solution in an AsynTask. You can also use context.
getFilesDir()
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
Returns the path of the directory holding application files.

Android: Save data to internal memory using Download

I am downloading file from a server in Android using the DownloadManager class. I want to store this file in the internal memory of the device. I tried to use .setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory() +"/Android/data/xxx.xxx.xxx/files/") as mentioned here, but it is not working. How to solve my problem?
add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to manifest.xml and create "/Android/data/xxx.xxx.xxx/files/" path
try this
File testDirectory = new File(Environment.getExternalStorageDirectory() +"/Android/data/xxx.xxx.xxx/files/");
and
if (!testDirectory.exists()) {
testDirectory.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