15

I have a file in the internal storage of my tablet. /myfolder/subfolder/index.html. How can i load this into a webview of an app.

i have tried

 webview.loadURL("file:///myfolder/subfolder/index.html"); 

but it is not giving the expected result. it says web page not available.

I know how to load from the asset folder or from the web/internet. but i need to load local file into webview. is it possible.?

SKK
  • 5,261
  • 3
  • 27
  • 39

5 Answers5

24
File file = new File("/data/data/packagename/foldername/");
webView.loadUrl("file:///" + file);
duggu
  • 37,851
  • 12
  • 116
  • 113
  • where does the folder name come from? Is it just a folder created by the developer or is it intrinsic to the internal storage? For example, if I create a file temp.pdf like this context.openFileOutput("temp.pdf", context.MODE_PRIVATE); will the file be stored in a folder or will the url simply be file:///data/data/packagename/temp.pdf ? – Rocket Garden Jan 18 '16 at 09:41
7

An app cannot access data from the Internal storage stored by another app. Permissions are applied to internal storage that make data written by an application not accessible outside of that application (your app cannot read anything written by another app).

So, if you are accessing a file that is not created by your app, AFAIK, you cannot have access to it.

BTW, you could access the file from the internal storage as below,

webview.loadURL("file:///data/data/com.yourproject.example/files/index.html");
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
  • thanks for that information. and i was able to load the html using "file:///mnt/sdcard/myfolder/subfolder/indexhtml" – SKK Jan 31 '13 at 12:06
  • i don't have a SD card. but, i used some file explorer, clicked on the html file, gave me option to select an application to open it. i gave browser and i found the path in the url entering area. – SKK Jan 31 '13 at 12:12
7

I was able to solve my problem by using the following as path:

webview.loadURL("file:///mnt/sdcard/myfolder/subfolder/index.html");
SKK
  • 5,261
  • 3
  • 27
  • 39
1

Put your html files in asset folder access the page like given below.

 webview.loadURL("file:///"+mContext.getFilesDir()+"/myfolder/subfolder/index.html"); 

you have to mention the android asset while accessing html pages in android assets.

Venkatesh S
  • 5,466
  • 1
  • 25
  • 30
1
  File gameDir = new File("/data/data/" + getActivity().getPackageName() + "/games");
    gameDir.mkdirs();

    // Create and execute the background task.
    mTask = new DownloadTask();
    mTask.execute("https://github.com/gabrielecirulli/2048/archive/master.zip", "/data/data/" + getActivity().getPackageName() + "/games/2048.zip");




    mWebView.getSettings().setJavaScriptEnabled(true);
                Toast.makeText(MainActivity.this, path+"/index.html", Toast.LENGTH_SHORT).show();
                mWebView.loadUrl("file:///data/data/com.example.zipfiledownload/games/2048-master/index.html");
Zafar Hussain
  • 264
  • 2
  • 10