Question

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(); 
        }
    }
Was it helpful?

Solution

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

OTHER TIPS

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");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top