creating folder in android emulator - android

I am developing an android application. I need to create a folder in the internal memory, but when I try to create the folder I get the error below. I am running in an emulator.
mkdir failed for /mnt/New Folder , read only file system
I have tried many paths, but still the error persists. The only folder that I am able to create is called "cache", but I cannot browse it by my file chooser activity.
Any idea where is the suitable place to create folders without any permissions?

You can achieve it by this from a Context object (like Activity).
File files_folder = getFilesDir();
File files_child = new File(files_folder, "files_child");
files_child.mkdirs();
File created_folder = getDir("custom", MODE_PRIVATE);
File f1_child = new File(created_folder, "custom_child");
f1_child.mkdirs();
The function
getFilesDir()
will get the folder data/data/yourpackagename/files in internal memory. And the function
getDir("custom", MODE_PRIVATE)
will create a folder name app_custom in your app internal folder.
Answered by Minhtdh

I guess what you call internal memory is atualy the external memory (which can be open by
file chooser activity, the real internal memory only can be open if you have rooted)
If that true, you should chek those belows:
- first, you will need the write storeage permission in Manisfest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- then you should use `
String path =
Environment.getExternalStorageDirectory().getAbsolutePath() +
"/yourfoldername"
`
than
mnt/yourfoldername
at last you should use mkdirs to create folder than mkdir

Related

Create a file folder for app

The below code doesn't create a folder in my device.
String intStorageDirectory = context.getFilesDir().toString();
File folder = new File(intStorageDirectory, "test");
folder.createNewFile();;
I need a folder created for my app to store media, when user installs it. That folder should be visible on file explorer. How can i do it?
With the current snippet you created a file, you can also create folder by creating file but your current directory is the base folder, getFilesDir() points internal storage for your app which not visible nor accessible unless explicitly declared. You can create a folder and file by creating with new File().createNewFile() or create only folder using mkdirs() but you won't be able to display it using a file explorer app and that folder and files inside it will be deleted when/if user uninstalls your app.
To save files externally(This doesn't mean saving to SD Card) you can create directory and file with
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), folderName);
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs()
}
File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + fileName);
And you need some kind of OutputStream to write data to that file.
Make sure that you ask <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> inside your AndroidManifest.xml file and ask write permission on runtime if your android:targetSdkVersion="23" or above
new File(context.getFielsDir(), "test").mkdirs();
createNewFile creates a file, not a folder. Using mkdirs instead of mkdir ensures that all parents exist. There's also no reason to go through a string when you already have a File.
Adding folder.mkdirs(); should work in place of folder.createNewFile(); And don't forget to add the permissions.
This will create a folder in you data directory.
And just a suggestion , if you want to store media in a SD card folder maybe Environment.getExternalStorageDirectory() is good.

How to create public directory in internal memory in android

I want to create a sub directory which is non-private under the Environment.DIRECTORY_PICTURES directory. I used the code shown below but of no avail. The directory is created but it remains private. I don't know where I'm wrong.
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyImages");
file.mkdirs();
File f = new File(file,"Image1");
First, you have not created a file, at least in the code that is shown above. You have created a Java File object, and you created a directory, but you did not create a file for Image1, and so your directory is empty. I know of no way to force your empty directory to be picked up by anything, though you should see it if you use adb shell or DDMS to examine your device.
When you do eventually write a file to this directory, be sure to call getFD().sync() on the FileOutputStream before you close() that stream. Then, use MediaScannerConnection and its static scanFile() method to have your newly-created file be indexed by the MediaStore. Until you do this, your newly-created file will not be visible via MTP or many third-party apps.

Where should I choose to save text file in android?

I hope to export my data as a text file and save it to disk in Android, so I need to choose which folder I will save the file to.
I hope that a normal user can find the folder easily and the app does not need special permission to create the folder.
I have read some document, it seems that there are 3 ways: Context.getFilesDir().getAbsolutePath(), Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null).
You know some android users don't install SD card, so it seems that Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) are be excluded.
Am I only to choose Context.getFilesDir().getAbsolutePath()? or is there a better way? Thanks!
BTW, From the document Android - Where to save text files to?
Save it in internal phone storage, here no users and applications can access these files(unless if phone is rooted). But these files will be deleted one's the user selectes clear data from Settings -> Apps -> .
It seems that normal users can't access the saved text files if I use Context.getFilesDir().getAbsolutePath(), is it right?
Use this if you want a path that the user can modify and can have access
getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
More documentation here.
EDIT:
This is how use in case error in some devices:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
String fname = "TEXT.txt";
File file = new File(path, fname);
if (!path.exists()) {
//noinspection ResultOfMethodCallIgnored
path.mkdir();
}
// YOUR CODE FOR COPY OR CREATE THE FILE TXT in PATH WITH THE VARIABLE file ABOVE

Android - copy files between internal storage

I would like to copy the files (image files captured from the camera app) between internal storage, from /data/data/[my app]/photo to another folder within the internal storage.
As you can see in the screen shot below, I have successfully changed the permission of both directories into '777' via the below code:
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("chmod 777 /data/data/[my app]/photo", null, new File("/data/data/[my app]/photo"));
So, I use the similar code to change the permission for the files in the "photo" folder but nothing happens.
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("chmod 777 /data/data/[my app]/photo/2_20130406_143344");
It would be very grateful if someone give me some direction as it has bothered me for the entire weekend.
Thanks a lot in advance!
If you are copying from one internal location to another and created by your app, you do not need additional permission. Try following
ContextWrapper wrapper = new ContextWrapper(context);
File sourceDirectory = wrapper.getDir(SOURCE_FOLDER_NAME, Context.MODE_PRIVATE);
File destinationDirectory = wrapper.getDir(DESTINATION_FOLDER_NAME, Context.MODE_PRIVATE);
Now use a recursive function to copy from source to destination
private boolean copyFiles(sourceDirectory, desitnationDirectory);
For, how to copy files recursively this.
Camera Images are stored on SD card and, you can always directly access those anytime.

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.

Categories

Resources