creating folder not visible in file explorer - android

Hi i am creating folder in external storage.first i need to check that Sd card is present or not.In my mobile i dont have sd card.i am using redmi mobile.Below is my code.I dnt have sd card bt i am getting sd card is mounted.Then i checked my internal storage i cant found the folder what i created.i had tried so many codes,please help me.i also added read and write external permission.
my code is:`
String state;
state=Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
Toast.makeText(this, "Sd card found", Toast.LENGTH_SHORT).show();
File root=Environment.getExternalStorageDirectory();
File dir=new File(root.getAbsolutePath()+"/myappfile");
if(!dir.exists()){
dir.mkdir();
}
}
else {
Toast.makeText(this, "SD card not found", Toast.LENGTH_SHORT).show();
}`

Try this code -
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
// yes SD-card is present
}
else
{
// Sorry
}
Source - https://stackoverflow.com/a/7429264/1649353
And also check Environment.isExternalStorageRemovable()

Try someting like this
if(Environment.MEDIA_MOUNTED.equals(Environnement.getExternalStorageState()))
{
File f = new File(Environment.getExternalStorageDirectory(), "/myappfile");
if (!f.exists()) {
if.mkdirs();
}
}

Related

How to create directories in /storage/extSdCard path in android java?

I can see sdcard folder contents by using this
File f = new File("/storage/extSdCard");
if (f.exists()) {
File[] files = f.listFiles();
if (files != null) {
for (File filz : files) {
Toast.makeText(this, filz.getName() + "", Toast.LENGTH_SHORT).show();
}
}
}
But when i try to create directories
File dir = new File("/storage/extSdCard/Android/Mayor");
try {
if (!dir.exists()) {
if (dir.mkdirs()) {
Toast.makeText(this, "Folder Created", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Folder Not Created", Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "WTF", Toast.LENGTH_LONG).show();
}
Its doesnt create at all. Any idea?
// create a File object for the parent directory
File myDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
myDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(myDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
Note:
It might be wise to use Environment.getExternalStorageDirectory() for getting the "SD Card" directory as this might change if a phone comes along which has something other than an SD Card (such as built-in flash, a'la the iPhone). Either way you should keep in mind that you need to check to make sure it's actually there as the SD Card may be removed.
UPDATE:
Since API Level 4 (1.6) you'll also have to request the permission. Something like this (in the manifest) should work:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You need to use DocumentFile.createDirectory(displayName) to create a directory on a removable SD card. See https://stackoverflow.com/a/35175460/1048340
File mnt = new File("/storage");
if (!mnt.exists())
mnt = new File("/mnt");
File[] roots = mnt.listFiles();
For read external sdcard, you need to mount sdcard path first then after you can able to use external sdcard path.

I want to check the phone contain SD card or not and following method always return true if SD card not available

I use the following method to check if phone conatins sd card or not but the issues is always return true if SD card not available please help me .
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
// yes SD-card is present
Toast.makeText(getApplicationContext(), "SD card available ", 2000).show();
}
else
{
// Sorry
Toast.makeText(getApplicationContext(), "SD card not available ", 2000).show();
}
It returns true because getExternalStorage() returns virtual SD card in internal memory.
You can use
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)
&& Environment.isExternalStorageRemovable();
to check is SD card removable or not
You can't check with Android API like Environment.*ExternalStorage* because they refer to the emulated sd card that is part of the data partition (so it will allways be mounted).
You can run "mount" shell command and try to find external sd card mount point, but is vendor (and sometimes device) spesific.
try to use this :
File file = new File("/mnt/extSdCard");
try
{
File list[] = file.listFiles();
Toast.makeText(getApplicationContext(), "Yes sdcard is mounted, file count "+list.length, Toast.LENGTH_LONG).show();
}
catch(NullPointerException o)
{
Toast.makeText(getApplicationContext(), "Sorry no sdcard is mounted:", Toast.LENGTH_LONG).show();
}

Reading file from SD card and loading it to WebView

i am trying to load a html file in webView from sd card its not working, Directory exists in SD card as well as file in it. Here is the code i have tried.
public void CheckReg()
{
File file = new File(getExternalCacheDir(), "Reginfo/input/register.html" );
if (file.exists())
{
index.loadUrl("file:///sdcard/Reginfo/input/register.html");
Toast.makeText(mContext, "File Exists", Toast.LENGTH_SHORT).show();
}
}
do this
File file = new File(Environment.getExternalStorageDirectory()+"/Reginfo/input/register.html");
if (file.exists())
{
index.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/Reginfo/input/register.html");
dont forget to
add permissions to menifest
should set like ,
public void CheckReg()
{
File file = new File(Environment
.getExternalStorageDirectory()
.getAbsolutePath()+"Reginfo/input/register.html" );
if (file.exists())
{
index.loadUrl("file:///"+file);
Toast.makeText(mContext, "File Exists", Toast.LENGTH_SHORT).show();
}
}
You shouldn't hard code the directory of the sdcard like that. Its typically at /mnt/sdcard/ but this is never assured instead of this you can write it like this.
and before you load the file from sd-card you make ensure that sd-card is mounted.
You can use the following:
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d(TAG, "No SDCARD");
} else {
index.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/Reginfo/input/register.html");
}

Why does my code to write to the sdcard work on one device but not on another device..?

Here is my code: The file never gets created on the SDCARD
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
logger.info("There is no external SD card mounted...stopping application");
Toast.makeText(MonitorService.this, "There is no external SD card mounted...stopping application", Toast.LENGTH_SHORT).show();
return;
}
final File root = Environment.getExternalStorageDirectory();
final File binaryfile = new File(root, "test.log");
writer = new FileWriter(binaryfile, true);
out = new BufferedWriter(writer);
if (root.canWrite())
{
System.out.println("I do see this in the logs....");
out.write("This is a test");
out.write("\n");
out.close();
writer.close();
}
The sdcard write only works if it is NOT mounted on the computer: is one of the devices automatically mounting the SD card? I've seen some of the vendor add-on services that automount the SD card. In general I'd expect root.canWrite() to work correctly, but you may want to use getExternalStorageState to verify that the SD card is indeed available.

To create a directory in Sd card inside an application

I want to create a directory on sd card keeping it as a separate activity in one of my application. I wrote the following code in the onCreate() of the application. It is not creating the directory though this code works fine if I try to implement it as an independent application.
Please suggest a solution for this problem.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
String dirName = "/sdcard/TEST";
File newFile = new File(dirName);
newFile.mkdirs();
Log.d("CaptureTest.java","Directory created");
if(newFile.exists()){
Log.d("capturetest.java","directory exists");
if(newFile.isDirectory()){
Log.d("capturetest.java","isDirectory = true");
}
else Log.d("capturetest.java","isDirectory = false");
} else
{
Log.d("capturetest.java","directory doesn't exist");
}
} catch(Exception e){
Log.d("capturetest.java","Exception creating folder " + e);
}
........................................
..........................................
}
The SD card might be mounted at /mnt/sdcard instead of /sdcard.
But the safest technique to get the external storage directory is like in the following code
File myDirectory = new File(Environment.getExternalStorageDirectory(), "my directory");
if(!myDirectory.exists()) {
myDirectory.mkdirs();
}
There could be a number of things causing this:
Check that external storage is available and writeable before trying to write to it.
Don't use String dirName = "/sdcard/TEST"; use Environment.getExternalStorageDirectory() or Context.getExternalFilesDir() instead.
This page has some really useful tips for correctly accessing the SD card.

Categories

Resources