Showing image in WebView from External storage Directory on device - android

I want to show an image in a webview which has been downloaded externally to ExternalStorageDirectory. I am using the followng code to load the HTML as a string with the base URL set to be that of where the images are saved:
Article article = page.articles.get(0);
String mime = "text/html";
String encoding = "utf-8";
String base = "file:/" + Environment.getExternalStorageDirectory() + "/images_folder/";
Log.v("IMAGE", base);
webView = (WebView)rootView.findViewById(R.id.webView);
webView.setBackgroundColor(Color.TRANSPARENT);
webView.loadDataWithBaseURL(base, article.articleContent, mime, encoding, null);
This code works fine in the emulator but when I try it on a device (Nexus 7 Tablet) the image doesn't load. The image is definitely stored in the External Storage Directory as they can be found using a file explorer on the tablet. I imagine this has something to do with the Nexus 7 using emulated storage but I really don't know, the call Environment.getExternalStorageDirectory() should point to the same directory every time.
The html String being brought in is very simple and looks like this:
<html><head></head><body><img src = 'test_download1.png'></img>This is the first Article for the first page in html</body></html>
Where test_download1.png is a file stored in the External Storage directory.

To access SDCARD in nexus 7 you have to write a different code as your code will give access only to mnt/sdcard and not to actual path,where your image is saved.
code to access SDCARD in nexus 7

Related

Trouble loading local html file into default device browser in Android app

I am an iOS developer assigned a task in Android, so bear with me, I'm a bit green in Android.
I am attempting to load a local html file that is stored in the device download directory in a folder called user_guide. I want the html file to load in the device's browser (not in a webview for reasons outside the scope of this post). I am using the following code to launch the browser:
String downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
String path = "file://" + downloadPath + "/user_guide/index.html"; // + R.raw.test;
Uri pathUrl = Uri.parse(path);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
browserIntent.setData(pathUrl);
context.startActivity(browserIntent);
I obtained the value of path by setting a breakpoint and manually set it in Chrome on my device to verify that is does work and loads the proper file. However, when I try to run this code in the app, I get the following toast message:
Cannot display PDF (index.html is of invalid format)
I'm confused about this message since I am trying to load an html file, not a PDF. Can anyone help me out? THanks!
Try changing "browserIntent.setData(pathUrl)" to
browserIntent.setDataAndType(pathUrl, "text/html")
to explicitly specify that it's HTML.
I found this suggestion at https://stackoverflow.com/a/7009685/10300291.

PDFTron Android Sample PDF

Using the Android sample in the WebViewer folder I have an application in Android Studio which when run on my device works and displays the xod file given as expected. However I've tried to change the lines:
String documentPath = getFilesDir().getPath() + "/GettingStarted.xod";
InputStream in = getAssets().open("xod/GettingStarted.xod");
to
String documentPath = getFilesDir().getPath() + "/sample.pdf";
InputStream in = getAssets().open("xod/sample.pdf");
And have also changed the endsWith(".xod") to endsWith(".pdf") but I only get a grey screen. For this to work does the file have to be a .xod? As it works for xod files but not pdf files.
Thanks for your time.
Yes, for mobile viewing you need to convert your files to the web-optimized XOD format.
The PDF backend for WebViewer is not available for mobile browsers. This is due to limitations of the hardware and the mobile browsers.
For desktop browsers, to switch from XOD backend to PDF backend, you need to follow the steps. See here for more details, especially if you run into any errors.
var myWebViewer = new PDFTron.WebViewer({
path: "lib",
type: "html5",
documentType: "pdf",
initialDoc: "GettingStarted.pdf"
}, viewerElement);
Notice the documentType parameter is set to pdf.

setting ImageView from image stored in sdcard in higher version of android not working

Toast.makeText(this, Environment.getExternalStorageDirectory(),
Toast.LENGTH_LONG).show();
File file = new File(Environment.getExternalStorageDirectory() +
"/whatsupv2/abc.jpg");
Bitmap mybitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageView.setImageURI(Uri.fromFile(file));
the above code is working for android 3.3 but not in 4.1.2 where we have two storage directories... i have checked abc.jpg is there in dir .. and path given is correct.. but imageview just show a white screen..
but not in 4.1.2 where we have two storage directoies
There is only one external storage directory in all Android devices, at least through Android 4.3.
First, make sure the image is in the official external storage location.
Second, use new File(Environment.getExternalStorageDirectory(), "/whatsupv2/abc.jpg") rather than string concatenation.
Third, either use decodeFile() or setImageURI(), not both.
Fourth, examine LogCat for any messages.

using files stored in the SDCard as resourses for internal website on a Browser

I need to download some resources from the net, then locally process them and show them on a BrowserActivity.
Since the resources (images) could be really big I'm saving them in the SDCard.
How may I refer to those stored resources from an html page? The html page is embedded into my app (It's in assets folder). I think this will fire some kind of security issues.
Is this the right approach? or is there any better solution?.
Thanks
Answer for question 1): You can refer to file on your sd card using "file://" prefix. See example below:
String fileName = "x"; // here is the file name of the downloaded image
String imagePath = "file://" + getApplicationContext().getFileStreamPath(fileName.getAbsolutePath();
String html = html+"<img src=\"" + imagePath + "\"/>" // embed that code in your modified html source.

Loading html file from local folder into webview

I'm new to Android development.
I want to load a html file into a webview.
Note that there are so many relevant questions on SO like this, but they all deal with getting **.html* from assets folder.
But I want to load html file from local folder, say "D://abc.html" because if my html is around 10Mb then corresponding apk size also goes upto 10mb.
Any help appreciated.
EDIT
I tried webView.loadUrl("file:///D:/Projects/myWebsite/index.html");
but it gives Web page not available and File not found error.
You can use:
WebView webView = // ...
webView.loadUrl("file:///myPath/myFile.html");
In an Android application, files can be read from 3 types of locations:
Internal storage: Each app has its own, file names are relative to this location. URL takes form file:///myFolder/myFile.html
External storage: Needs permission and may not always be available. Get the root folder by calling Environment.getExternalStorageDirectory(). So, construct the URL using: String url = "file:///" + Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html"
Assets: Stored in the apk. Read-only access. URL takes form file:///android_asset/myFolder/myFile.html (See also Loading an Android Resource into a WebView)
In Android 4.4 KitKat, a "Not allowed to load local resource: file:///.." is thrown.
arise when loadURL and the only alternative I've found is "loadDataWithBaseURL".
webView.loadDataWithBaseURL("file:///android_asset/demo/",
tmpDocumentText,"text/html", "UTF-8", null);
WebView has loadData method http://developer.android.com/reference/android/webkit/WebView.html
All you need to do is reading the file into String then feed it to WebView using loadData.
either one can use this way to load file specifically if the file is present within android studio project
(pls note android_res is res folder actually and should not be replaced)
webView.loadUrl("file:///android_res/drawable/FILENAME.EXTENSION");
but if that is not working please try another way of loading file like
webView.loadDataWithBaseURL("file:///android_asset/demo/",tmpDocumentText,
"text/html", "UTF-8", null);

Categories

Resources