About Android webview file upload - android

I am using webview in android app, the webview will load a html page contains file upload control.I override the "openFileChooser" function, so I can select file in webview,but after I choose file and return to webview, the file name while change to url-encoded format(If the file name isn't ascii character) ?

There's nothing you can (or should) do here.
The operating system handles these things, i.e. placing the selected file in the value attribute of the <input type="file">.
But there's nothing wrong with these URL-encoded names. You shouldn't rely on the filenames from the client side, anyway.
What you should use is only the file's content and maybe its size and extension as reported.

Related

How to create an editable copy of a password-protected PDF on Android?

Context
I'm using a third-party library called PsPdfKit to edit PDF files, adding annotations - like PNG images, and text decorations - on top of the PDF document. But there's a limitation of this library where I'm not able to use this annotation feature on password-protected PDF files. I can open and see the documents, but I'm unable to actually drop these annotations in.
What I'm trying to do now is to figure out whether could be possible to create an editable copy of the PDF file.
Question
Is there any way on Android to create an editable copy of a password protected PDF file? Again, these password protected PDF files are only preventing writing any changes on top of the PDF, you don't actually need a password to see the PDF content.
My idea is to create an editable copy of the PDF file and then pass that copy to the PsPdfKit library.
Okay, so I've finally figured out a workaround for this issue. What I did was to instantiate the password protected PDF file via PsPdfKit and then create a writeable copy of the file using the page bitmaps of the password protected file. A bit of a hacky solution, but it did allow me to use the annotation feature.
// We open the password-protected file.
val readOnlyPdfDocument = PdfDocumentLoader.openDocument(applicationContext, readOnlyFile.toUri())
// Use the PsPdfKit API to create a copy of the pages of this document
val task = PdfProcessorTask.newPage(NewPage.fromPage(readOnlyPdfDocument, 0).backgroundColor(Color.RED).build())
for (i in 1 until readOnlyPdfDocument.pageCount) {
task.addNewPage(NewPage.fromPage(readOnlyPdfDocument, i).backgroundColor(Color.RED).build(), i)
}
// Finally store the writeable PDF document in a new file
val writeableFile = File(applicationContext.cacheDir, "Writeable.pdf")
PdfProcessor.processDocument(task, writeableFile)

Load html in a webview from res/raw based on locale

using this code
WebView wvChangelog = (WebView) alertDialog.findViewById(R.id.wv_changelog);
wvChangelog.loadUrl("file:///android_res/raw/changelog");
I am able to load and show a html file from the res/raw folder, what I need to do is to load files from respected locale folders eg raw/changelog , raw-de/changelog, raw-fa/chenglog I believe the same snippet should work, but it only loads the main file (located in raw folder). although other locale related stuff in the same context (eg the title string loaded by getString(R.string.title) is respecting the locale.
Am I missing something here?
also I'm aware of InputStream inputStream = getResources().openRawResource(R.raw.filename); but as you can see the WebView does not accept Input Stream and I have to comeup with a file:///android_res... type
I solved this problem by creating multiple raw folders. Then to load each one specifically according to the language , That's the phone's language has to be changed, Settings >> Language and Input. Then it will choose the files accordingly.

Use IFrame in Android Webview to view PDF file? [duplicate]

I have an Android app that displays a comic book. To make use of the built-in zoom controls, I am loading the pictures in a WebView like so:
webView.loadUrl("file:///android_asset/page1.jpg");
This is working just fine, however, since the images are in the assets folder, they are not being compressed which makes my .apk enormous. I was wondering how to reference resource files (from the res/drawable folder) with a file path like I did above with the assets. Does anyone know what that path would look like? I've tried things like "file:///res/drawable/pagetitle.jpg" with no success. Thanks for the help.
Update:
I found that "file:///android_res/drawable/page1.jpg" was the path that I was looking for.
from this site
Using the resource id, the format is:
"android.resource://[package]/[res id]"
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);
or, using the resource subdirectory (type) and resource name (filename without extension), the format is:
"android.resource://[package]/[res type]/[res name]"
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");
I also had to use loadDataWithBaseURL instead of just plain loadData to get the file:///android_res/drawable/page1.jpg to work.

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

How to retrieve file content using full path?

In Android webview, you could pass something like this file:///android_asset/myfile.txt or this http://www.mysite.com/t.html and it will get it without a problem. Now if I tried the first url with the file prefix in a reader "FileReader" it will throw an exception that File not found although in WebView it work with no problem (why?)
What I want is to create a function that could take a file or http url and get the content html using the full path "file:/// ..." how could I do this without facing the file not found exception ?
Now if i tried the first url with the file prefix in a reader "FileReader" it will throw an exception that file not found although in WebView it work with no problem (why?)
Because "File Reader" does not have an asset named /myfile.txt, presumably. "File Reader" is somebody else's program, not yours, so file:///android_asset/myfile.txt refers to an asset in that other program, not yours.
What i want is to create a function that could take a file or http url and get the content html using the full path "file:/// ..." how could i do this without facing the file not found exception ?
Have an asset named /myfile.txt in your app.
I have not tried using standard Java I/O (e.g., File objects) to read file:///android_asset paths. That might work. If not, use startsWith() to determine if the string begins with file:///android_asset -- if it does, trim that off and use the rest with AssetManager to read the asset.

Categories

Resources