Tracking a path of sd card file programatically - android

I have put html file in sd card on android device. and i am running that through webview. But the path that i am giving in emulator is working fine. but not on device. Its giving an error ** Web Page on Found** while running.
I have this code to find the sd card availability and sd card root directory path and it is working fine. and output is coming properly.
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
Toast.makeText(this, "yes SD-card is present", Toast.LENGTH_SHORT).show(); }
else
{
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
}
File externalStorage = Environment.getExternalStorageDirectory();
externalStorage.getAbsolutePath();
Toast.makeText(this, externalStorage.getAbsolutePath(), Toast.LENGTH_SHORT).show();
but can i get the proper whole file path. Please suggest.

this code definitely solve your problem
String storage_path = Environment.getExternalStorageDirectory().toString()
+ File.separator
+ vfile;
also if you want to add some files in sd card then use this permission in your manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

may be this can be help you, try it.
String path = Environment.getExternalStorageDirectory().toString() + "/Filename_with_extension" ;

Related

Android doesn't create folder on my Nexus

I have a Nexus 9 device. I would create a personal folder in the /sdcard/ path, something like this:
/sdcard/MyFolder/
so i coded this:
File directory = new File("/sdcard/MyFolder/");
if(directory.mkdirs()){
Toast.makeText(context, "Folder created", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Folder not created", Toast.LENGTH_SHORT).show();
}
When I launch the app, it shows me "Folder not created" and so it doesn't create folder called MyFolder into /sdcard/ path. In Nexus 9 device the /storage/emulated/0path doesn't exist, so I have to use /sdcard/ path to accessing my storage.
I also used permission in my AndroidManifest.xml file, in this way:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Can you tell me please what's wrong?
Thanks
Since lots of devices have different file structures it is safer to use Environment.getExternalStorageDirectory() for determining the SD card path.
Can you try this code
File folder = new File(Environment.getExternalStorageDirectory() + "/MyFolder");
if (!folder.exists()) {
folder.mkdir();
}
try to specify external storage as listed below:
File directory = new File(Environment.getExternalStorageDirectory().toString()+"/MyFolder");

Unable to read directories or files from a sd card

I am new to android development and I have tried to read a directory from my sd card but didn't got succeeded. Below is the code which I wrote to achieve it
File sdcard = Environment.getExternalStorageDirectory();
if(sdcard.exists()) {
Log.d("Sd card", "Sd card exist");
File[] file_names = sdcard.listFiles();
for(File x : file_names) {
Log.d("File Name",x.getName());
}
}
Control passed the if condition and then I got an NullPointerException at "for each" loop line. Probably function sdcard.listFiles() is returning null. I have an Sd card with many folders and files. Actually I have to create a directory file object for directory "Attachments" which is available on my sd card. I also tried the following code to achieve the same.
File file = new File(android.os.Environment.getExternalStorageDirectory() + File.separator + "Attachments");
and
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Attachments");
on both code when I called the function file.exists(), it returned false.
I have also checked whether sd card is mounted or not by the following code.
String sdCardAvail = Environment.getExternalStorageState();
if(sdCardAvail.equals(Environment.MEDIA_MOUNTED))
Log.d("Card status", "Sd card Available");
The above code has printed "Sd card Available" at log cat.
So please help me out to know whether I am doing it correct or missing any thing.
Thanks....
In order to read or write files on the external storage, your app must acquire the READ_EXTERNAL_STORAGEorWRITE_EXTERNAL_STORAGE system permissions. For example:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
If you need to both read and write files, then you need to request only the WRITE_EXTERNAL_STORAGE permission, because it implicitly requires read access as well.
Also See This Documentation. It Will solve all your future regarding Storage problems.

Android Create Directory to device

I use the following to check if a directory exists and if it doesn't it then creates it
final String appPath = String.format("%s/DataFiles", Environment.getExternalStorageDirectory());
File f = new File(appPath);
if(f.exists() && f.isDirectory()){
Toast.makeText(getBaseContext(), "Exists", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getBaseContext(), "DOES NOT Exist", Toast.LENGTH_LONG).show();
f.mkdir();
This works fine on my S3 that has a sd card inserted
When I try it on anither phone that doesnt have an sd card in it recognises that the directory doesnt exists and then doesnt create the directory
Any ideas whats wrong
Mark
BTW I have tried inserting an sd card but doesn't make a difference it still doesn't create directory.
Before any operation you need to check if external storage is available.
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
if sd-card is not available or is not available for writing you need to use device storage:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

android Downloading database to phone WITHOUT SD card

I have read this tutorial about downloading database to my sd card and I have a question what will happen while the mobile phone does not have SD card?
Then I have to download data to internal memory.
So how to check if device has got sd card or not and then set appropriate location before downloading? or maybe it will be done automatically because it use:
outFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
Please help me if you know,
Thank you
Some of Android phones doesnt have an sdcard slot but the internal memory is treated and simulated as external sdcard. Check this before.
To check the sd-card state: getExternalStorageState()
If it has sdcard (or simulated sdcard)
outFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
If its better if you create a folder in the sdcard:
String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
File dir = new File(sdpath + "/AppFolder");
dir.mkdir();
outFile = new File(dir.toString() + "/" + fileName);
IF the phone dosnt have a simulated sdcard:
outFile = new File(fileName);
The location is data/data/mypackage/
I think it can be useful Android Storage Options.
For database you can check sd-card state with getExternalStorageState(), or use Android SQL helper-class.
public static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

Problem to create directory in Android

I'm developing a simple application for Android devices.
I need to create a directory, but I can't do it.
File folder = new File(Environment.getExternalStorageDirectory ()+"/dir");
if(folder.mkdirs()){
CrearToast("Directorio creado"); //successfully created
}else{
CrearToast("fallo"); // error creating directory
}
*CrearToast creates a toast with the text in brackets.
I have set
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
are you trying to write on SD card or phone memory??
Have you looked at this??
http://developer.android.com/guide/topics/data/data-storage.html
EDIT:
This is how i create my folders
File CheckDirectory;
CheckDirectory = new File(FolderPath);
if (!CheckDirectory.exists()){
CheckDirectory.mkdir();
}
SD card directory:
Environment.getExternalStorageDirectory().getAbsolutePath() + "/";

Categories

Resources