Reading file from SD card and loading it to WebView - android

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");
}

Related

Android check existence of file in android internal storage

I put some .mp4 file in a folder in my android device internal storage. And then I play this file from my application. It plays well if exists otherwise app crushed.
So before play file I want to check its existence.
I tried below code but no luck.
File file = new File("file:///storage/emulated/0/tutorial/1a1cbfc4-18cb-4637-8405-01bf9bebeda3.mp4");
if (file.exists()) {
LogUtil.printLogMessage(VideoListActivity.class.getName(), "video File", "file exist");
} else {
LogUtil.printLogMessage(VideoListActivity.class.getName(), "video File", "file not exist");
}
i am sure the file is exist in the folder named tutorial in my internal storage.
Use file.isFile() for file & file.isDirectory() for directory,
file.exists() tries to access the file which causes it to crash if the file doesn't exist, file.isFile() uses linux stat, which only returns the information about the file without trying to access it.
Please make proper file name, Replace with File myDirectory = new File(Environment.getExternalStorageDirectory(), dirName);
public Boolean checkFile(){
File file = new File("/storage/emulated/0/tutorial/1a1cbfc4-18cb-4637-8405-01bf9bebeda3.mp4");
if (file.exists()) {
return true;
} else {
return false;
}
}
Use checkfile True/False flag before video play.
Try this
File file = new File(path);
if (!file.isFile()) {
}else{
}

creating folder not visible in file explorer

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();
}
}

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.

How to Create a folder in external sdcard in Android 4.4.2(Kitkat) API level 19

I try a lot to create a folder in SD card in android 4.4.2 version.
i try the following code
String dirName="Test";
File file = new File(Environment.getExternalStorageDirectory(), dirName);
boolean status = file.mkdir();
if (status)
Toast.makeText(MainActivity.this,
"Directory created successfully", Toast.LENGTH_SHORT)
.show();
else
Toast.makeText(MainActivity.this, "Directory create failed",
Toast.LENGTH_SHORT).show();
but it creates a folder in internal storage.
and I try another code is
String path=System.getenv("SECONDARY_STORAGE");
File file=new File(path +"/Test123");
file.mkdir();
it creates a folder in External SDCARD in android 4.1 but not in android 4.4.2
So how can i create a folder in External sdcard in android 4.4.2??
If anyone know please help me..
Thanks in advance
The documentation states that
Applications should not directly use this top-level directory, in
order to avoid polluting the user's root namespace. Any files that are
private to the application should be placed in a directory returned by
Context.getExternalFilesDir, which the system will take care of
deleting if the application is uninstalled.
where with top-level directory they mean the return value of Enviroment.getExternalStorageDirectory()
Using getExternalFilesDir you will have
File file = new File (getExternalFilesDir(null), dirName);
if (!file.exists()) {
boolean status = file.mkdir();
if (status) {
Toast.makeText(MainActivity.this, "Directory created successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Directory create failed", Toast.LENGTH_SHORT).show();
}
}
also you should be aware of the fact that mkdir() returns false if the directory already exists
This should be self-explanatory.
String folderName = "NewFolder";
File file = new File(Environment.getExternalStorageDirectory(),
folderName);
if (!file.exists()) {
file.mkdirs();
}
Make sure you have added WRITE_EXTERNAL_STORAGE permission in your manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Credits to Dhaval

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