I have this code:
File rootsd = Environment.getExternalStorageDirectory();
File[] imagelist = rootsd.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});
And my permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Somehow the imagelist is null instead of empty. Can anyone help me. Thanks a lot.
move rootsd file upto the directory where images are present. you are just considering /mnt/sdcard path. point to the directory where images are in, like
File rootsd = new File(Environment.getExternalStorageDirectory().getPath()
+ File.separator + "imagesdirectory");
Related
Why does this code only create a new folder in the internal storage and not create the folder on the SD Card.
Thanks
Code:
File direct = new File(Environment.getExternalStorageDirectory()+"/Akshay");
if(!direct.exists()) {
if(direct.mkdir()); //directory is created;
}
Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Try this :
File f = new File(Environment.getExternalStorageDirectory(), 'Akshay');
if (!f.exists()) {
f.mkdirs();
}
I have a problem with creating files in Android. I have followed this tutorial, and wrote this method:
public File getStorageDir(String name) {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).toString();
File file = new File(path, name);
System.out.println(path);
if (!file.mkdirs()) {
System.out.println("Directory not created");
}
return file;
}
Path variable prints /storage/emulated/0/Documents, however if goes off and no such directory is created. I have added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
permissions to manifest file. I have tried using file.getParentFile().mkdirs() but got same result. What am I doing wrong?
You use below code to create folder.
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "folderName");
if (!folder.exists()) {
success = folder.mkdirs();
}
So turns out it was being created the whole time, just not visible in file explorer. I have fixed it with answer from this post. Final code looks like this:
public File getStorageDir(Context context, String name) {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath();
File file = new File(path, name);
file.getParentFile().mkdirs();
MediaScannerConnection.scanFile(context, new String[] {path}, null, null);
return file;
}
Thanks everyone for answers, hope this helps someone who faces same problem.
I need to save picture to a Gallery, just to a normal place where the Photos are going. On SO I've found that Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) is internal, no matter that it says ExternalStorage.
But mkdirs always return me false.
String mediaStorage =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Camera";
File mediaStorageDir = new File(mediaStorage);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Toast.makeText(MainActivity.activity, "failed to create directory", Toast.LENGTH_SHORT).show();
return null;
}
}
Is there an easy way to save pics to an internal Gallery directory?
In case, my manifest has :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
This is what I tried.
private void createFolderInExternalStorage() {
String storagePath = System.getenv("SECONDARY_STORAGE");
Log.e("storagePath->",storagePath);
String path = "not available";
if (storagePath != null) {
Log.e("Path->", "" + storagePath);
File file = new File(storagePath.toString());
Log.e("readable->", "" + file.canRead());
Log.e("writable->", "" + file.canWrite());
Log.e("executable->", "" + file.canExecute());
dir = new File(storagePath + File.separator+etFolder.getText().toString());
if (!dir.exists()) {
dir.mkdirs();
Toast.makeText(this,"Folder "+etFolder.getText().toString()+" created",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"Folder "+etFolder.getText().toString()+" already exists",Toast.LENGTH_SHORT).show();
}
path = dir.getPath();
} else {
Toast.makeText(this,"External Storage not available",Toast.LENGTH_SHORT).show();
}
tv.setText("External SDCARD path->" + path);
}
if Secondary storage is present then System.getenv("SECONDARY_STORAGE") return /storage/sdcard1 in my case but getting following:
03-21 12:02:26.827 14155-14155/com.think.teststorage E/readable->: false
03-21 12:02:26.827 14155-14155/com.think.teststorage E/writable->: false
03-21 12:02:26.828 14155-14155/com.think.teststorage E/executable->: false
Even in some devices getting the above status as true but folder creation fails.
I have added the permission:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Suggestions are welcome.
You can use this code to create a folder.
File dir = new File("path/of/your/folder");
try{
if(dir.mkdir()) {
System.out.println("Folder created");
} else {
System.out.println("Folder is not created");
}
}catch(Exception e){
e.printStackTrace();
}
Add this permission also :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
For further reference : see this link
Let me know if this works for you! :)
Use the following lines:
//Define the path you want
String path = Environment.getExternalStoragePublicDirectory(Environment.YOUR_DIRECTORY) + File.separator + "YourFolderName";
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
YOUR_DIRECTORY is the directory where you want to create the folder, for example: DIRECTORY_DOCUMENTS, DIRECTORY_DOWNLOADS, DIRECTORY_PICTURES etc.
In your manifest should to add permission for write:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Hope it help!
It is very simple and straightforward in android
`
File mFolder = new File(Environment.getExternalStorageDirectory(), "Folder_Name");
if (!mFolder.exists()) {
mFolder.mkdirs();
mFolder.setExecutable(true);
mFolder.setReadable(true);
mFolder.setWritable(true);
}
`
Also include required permissions in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Just put these lines of code,
// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
and add require permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Does not work on KitKat, external, physical sdcard ??
then use use
Environment.getExternalStorageDirectory().getPath();
I am unable to get the methods for creating folder in Internal Memory,
i gone through few conversations in Android create folders in Internal Memory and Problem facing in reading file from Internal memory of android. But still i am unable to meet my requirement.
My requirement is , I want to create a folder in Internal Memory, there i want to Store one video.
Thankyou you very much in advance for valuable feedbacks.
try the below
File mydir = context.getDir("users", Context.MODE_PRIVATE); //Creating an internal dir;
if (!mydir.exists())
{
mydir.mkdirs();
}
Here is the code which I am using for creating files in internal memory :
File myDir = context.getFilesDir();
// Documents Path
String documents = "documents/data";
File documentsFolder = new File(myDir, documents);
documentsFolder.mkdirs(); // this line creates data folder at documents directory
String publicC = "documents/public/api." + server;
File publicFolder = new File(myDir, publicC);
publicFolder.mkdirs(); // and this line creates public/api.myservername folder in internal memory
To create directory on phone primary storage memory (generally internal memory) you should use following code. Please note that ExternalStorage in Environment.getExternalStorageDirectory() does not necessarily refers to sdcard, it returns phone primary storage memory
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "MyDirName");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("App", "failed to create directory");
return null;
}
}
Directory created using this code will be visible to phone user. The other method (as in accepted answer) creates directory in location (/data/data/package.name/app_MyDirName), hence normal phone user will not be able to access it easily and so you should not use it to store video/photo etc.
You will need permissions, in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
File direct = new File(Environment.getExternalStorageDirectory()+"/folder_name");
if(!direct.exists()) {
if(direct.mkdir()); //directory is created;
}
There is a "cacheDirectory" in your "data/package_name" directory.
If you want to store something in that cache memory,
File cacheDir = new File(this.getCacheDir(), "temp");
if (!cacheDir.exists())
cacheDir.mkdir();
where this is context.
try {
File cashDir = new File(dir.getCanonicalPath(),"folder");
if(!(cashDir.exists())) cashDir.mkdirs();
} catch (IOException e) {
e.printStackTrace();
}