I want to create Folder in below path
Internal storage/Android/media/{package name}/myfolder
Can anyone ome help me to create "myfolder" ?
You can use adb shell to access the shell.
Then you can navigate to the required directory using cd command, then mkdir to create the directory.
Using This Function you can Get your required Result.
private File createDirectory(String dirName) {
File file = new File(getApplicationContext().getExternalFilesDir(dirName) + "/" + dirName);
if (!file.exists()) {
file.mkdir();
}
return file;
}
Related
I would like to save some files in different directories under Android/data/data/my_app_package/files directory. I am able to obtain the path to the "files" directory by calling context.getExternalFilesDir(null), but can't create a directory under the path returned. Does anyone know why?
Thanks,
File root = SoundApplication.getAppContext().getExternalFilesDir(null);
File soundDir = new File(root, "/sound-dir");
if (!soundDir.exists()) {
boolean dir = soundDir.mkdirs();
System.out.println("dir = " + dir);
}
soundDir.mkdirs() returned true, but I don't see "sound-dir" in Android.data.mayPackage.files directory with Windows explorer.
I am trying to access external storage in Android.
I want to push some images so that I can use this in my application.
The code I am using as follows:
File directory = new File(String.valueOf(android.os.Environment.getExternalStorageDirectory())
+ File.separator + AppConstant.PHOTO_ALBUM);
PS: PHOTO_ALBUM is defined as "papu"
It returns /storage/emulated/0/papu
I cannot see this path exists through DDMS, file explorer but this condition is true;
if (directory.isDirectory()) {
}
Anyways, I assumed that it is reading from mnt/shell/emulated/0/papu
When I try to get all files through
File[] listFiles1 = directory.listFiles();
It does not return any files at all.
How can I push files when I don't this folder in the DDMS explorer ?
Here is how the DDMS looks like:
And my code is:
File directory = new File(String.valueOf(android.os.Environment.getExternalStorageDirectory())
+ File.separator + AppConstant.PHOTO_ALBUM);
// check for directory
if (directory.isDirectory()) {
File[] listFiles1 = directory.listFiles();
i have used getFilesDir() to create a folder in the application directory, it gives the path of the applicatoin directory as follows
/data/data/{my application package}/files
but when i use it to create a new folder using
File folder = new File(getFilesDir()
+ "/MyFolder");
if (!folder.exists()) {
folder.mkdir();
}
i don't see any folder. Also when i access in ES Explorer the actual path of the application directory is
/Android/data/{my package name}/files
My question is how to create a folder in the application directory so that it can be deleted automatically on application uninstallation.
Use method Context.getDir() instead. You don't need to invoke mkdirs(), because getDir() will do it automatically.
Quote from the documentation:
Retrieve, creating if needed, a new directory in which the application
can place its own custom data files. You can use the returned File
object to create and access files in this directory. Note that files
created through a File object will only be accessible by your own
application; you can only set the mode of the entire directory, not of
individual files.
Use this by making a use of getDir()
File dir = ctx.getDir("my_sub_dir", Context.MODE_PRIVATE);
File newfile = new File(topDirFile.getAbsolutePath() + File.separator + "new_file_name");
newfile.createNewFile();
BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(newfile));
I'm currently trying to natively call on an executable on the shell from my Android Project which is in my application home directory. I can see via the command line when using ADB that my file is not getting executable file permissions, however. I've tried the following:
File file = new File(myFileLocation);
if(file.exists()){
boolean executable = file.setExecutable(true);
}
'executable' remains false.
I've also tried the following:
Process processChmod = Runtime.getRuntime().exec("/system/bin/chmod u+x " + myExecutableLocation);
processChmod.waitFor();
When I try to issue a command on the process, I get the following IOException:
java.io.IOException: Permission denied java.io.IOException: Error
running exec(). Command: [/storage/emulated/0/myApp/iperf, -c,
iperf.eltel.net] Working Directory: null Environment: null
The command I'm trying to issue is:
Runtime.getRuntime().exec("/storage/emulated/0/myApp/iperf -c iperf.eltel.net")
Again, at this stage, I can see that the file permissions on the process I wish to use is simply r/w and not executable. Any help appreciated! By the way, I'm trying to use the 'iPerf' C library and there is a compiled armeabi module as well as the original sources/JNI code. There are no compilation issues and this looks like a file permissions issue more than anything.
Files located on SD card aren't executable. Move the file to internal storage (e.g. to /data/local or, if you get permission denied, to data/local/tmp).
That is how I did it :
String filePath = getFilesDir().getAbsolutePath() + File.separator + "myFileName";
File myFile = new File(filePath);
if (!myFile.canExecute()) {
Log.d("File is not executable, trying to make it executable ...");
if (myFile .setExecutable(true)) {
Log.d("File is executable");
} else {
Log.d("Failed to make the File executable");
}
} else {
Log.d("File is executable");
}
I think Android's chmod does not understand u+x syntax, try using the numeric notation like 744.
I need to create files under myapp/files/subdir with global permission in my application. I do this because I use external applications to open some files
Using this
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
creates file only under files folder. Using
File dir=new File(Constants.TASK_DIRECTORY);
dir.mkdirs();
File file=new File(dir, FILENAME);
file.createNewFile(); FileOutputStream fos=new FileOutputStream(file);
creates files under subdirectories but with private permissions. I need to find a way to compose those both to create a file in a subdirectory to be world readable
I have been trying a lot of things but none helped me and this was the longest time unanswered question of mine
I know this is an old question, but here is the correct way
public void makeFileWorldReadable(String path)
{
File f = new File(path);
if(!f.exists()) // Create the file if it does not exist.
f.createNewFile();
f.setReadable(true, false);
}
OP asked how to give access to a file in the following hierarchy: appdir/files/subdir/myfile.
The answers provided here don't take subfolder into account, so I feel there's a room for improvement.
In order to access file in hierarchy, a consumer should have execute permission on each folder in a path in order to access (read, write, execute) files underneath it.
For API >= 24
Starting from API 24, Android restricts access to appdir (a.k.a /data/data/appdir):
In order to improve the security of private files, the private
directory of apps targeting Android 7.0 or higher has restricted
access (0700). This setting prevents leakage of metadata of private
files, such as their size or existence.
The appdir doesn't have world-execute permission, and therefore you can't cd into it:
angler:/ $ cd /data/data
angler:/data/data $ cd com.myapp
/system/bin/sh: cd: /data/data/com.myapp: Permission denied
Bottom line: you can give world-readable permission to one of the files in your app's folder, but no other app (as long as they don't share the same Linux user ID) will be able to read them.
Not only that: attempt to pass a file:// URI to external app will trigger a FileUriExposedException.
For API < 24
The appdir folder has world-execute permission by default:
shell:/ $ cd /data/data
shell:/data/data $ cd com.myapp
shell:/data/data/com.myapp $
Note that even the appdir/files folder has world-execute permission:
shell:/data/data/com.myapp $ cd files
shell:/data/data/com.myapp/files $
But if you'll try to create a sub-folder (underneath files folder), using this code:
File subdir = new File(context.getFilesDir(), "subfolder");
subdir.mkdir();
it won't have world-execute permission:
shell:/ $ cd /data/data/com.myapp/files
shell:/data/data/com.myapp/files $ cd subfolder
/system/bin/sh: cd: /data/data/com.myapp/files/subfolder: Permission denied
shell:/data/data/com.myapp/files $ run-as com.myapp
shell:/data/data/com.myapp $ cd files
shell:/data/data/com.myapp/files $ ls -l
total 72
drwx------ 3 u0_a226 u0_a226 4096 2016-11-06 11:49 subfolder
shell:/data/data/com.myapp/files $
Therefore, you have to explicitly give your newly created folder world-execute permission using File#setExecutable method (added in API 9):
subdir.setExecutable(true, false);
And only then, as suggested by others, you can create your file and give it world-readable permission:
File file = new File(subdir, "newfile");
if(!file.exists()) {
file.createNewFile();
file.setReadable(true, false);
}
Doing that will allow any external application read your file:
shell:/ $ cd /data/data/com.myapp/files
shell:/data/data/com.myapp/files $ cd subfolder
shell:/data/data/com.myapp/files/subfolder $ cat newfile > /sdcard/1
shell:/data/data/com.myapp/files/subfolder $ cd /sdcard
shell:/sdcard $ ls 1
1
If you are running it on a rooted device, change file permissions using:
Runtime.getRuntime().exec("chmod 777 " + PATH + fileName);
Or try File.setReadable(), File.setWritable while creating the file.
One workaround I used in the past was to re-open the already existing file using openFileOutput in append mode, and pass in the world readable and/or world writable flags during that time. Then immediately close the file without writing to it.
I like the new methods added in API 9 better though.