I have a problem with loading local html pages to webView in android 4. Before, I tested it for android 2.3 and it was ok. I load the page like this webView.loadUrl(file:///android_asset/drawable/test.html) I read a lot of information about bug with loading local pages, but I have not solution of this problem. Can you explain me why it doesn't load pages and write that "Web page not avaliable" . May be problems with "file:///" or "android_asset". Please give me real advice how I can load my html pages to webview in android 4. I really need. Thank you.
P.S I am sure that the file exists in that folder.
I resolved my problem. I used:
private final int boundaryApiValue = 10;
/**Function set root directory for current api version.*/
private void setRootPathForCurrentVersionOfAndroid() {
if (Build.VERSION.SDK_INT >= boundaryApiValue)
rootPath = "file:///android_res/drawable/";
else {
rootPath = "file:///android_asset/res/drawable/";
}
}
If u have a version lower than Android HoneyComb than u should load html files for webview from "file:///android_asset/res/drawable/" if u have HoneyComb and later than use "file:///android_res/drawable/" It's works for me fine.
Related
I have a doubt, how I can delete a folder on Android 11 (or 10)?
Have much answers here on Stack how to do it, but nothing of worked.
This code worked for me on Android 5:
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
On newest versions of Android it not work. I noticed that there are applications that can to do this, so I imagine it is possible. Any answer about it?
Android has done much to change its permission models around file access since Android 10 and 11. The preferred approach is to use Scoped Storage APIs.
Many of the old file read/write permissions are on life support and will no longer work. If they do continue to work, then must justify to Google why you need them, and then go through a security approval by Google. You will only be granted approval if you app is classified as a file browser application, or the like.
Basically, the new model is to use Android's file browsers to gain access to read/write/update a particular file that the user selects, and defer the rest of the file management to Google's first-party applications. The access you get is based on what the user selected in the first-party file browser. You are then handed a URI back to your application with the proper permissions to perform the intended action, such as read/write/etc...
You may find this video useful:
https://www.youtube.com/watch?v=RjyYCUW-9tY
Have you tried Context.deleteFile() ?
getApplicationContext().deleteFile(filename);
Android 11,
I'm trying to create a publicly accessible folder I can store my media files within, which will contain 1 folder and 1 text file per game type (there could be many), I do not want any other application to have access to the root folder for the exception of file explorers, as the user 'could' have content inside the folder that is R18 restricted or might not, I'm not in control of what content goes in there, it would be nice if I could provide that so that a child doesn't in mistakenly bump into the content while looking for pictures on the device for barnie.
I found some code on GitHub that did just the above on Android 10 and works flawlessly for Android 10, but cannot find anything but blue pills about it when it comes to Android 11, help in the form of example code would be great, yes I know targeting only Android 11 limits me but I'd rather live with the limit than play with many different versions of code.
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
}
startActivityForResult(intent, OPEN_FOLDER_REQUEST_CODE)
EDIT 2
After some hacking around, because 'startActivityForResult(Intent!, Int): Unit' is deprecated. Deprecated in Java, I end up with this, results is a URI, can I now use this to pass files to other API calls now?
pref = getSharedPreferences("myPref", Context.MODE_PRIVATE)
var userFolderData: String? = pref.getString("userFolderData", "")
if (userFolderData=="") {
val getUserFolderData =
registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) {
mytools.debug(it.toString())
pref.edit().putString("userFolderData",it.toString()).apply()
userFolderData = it.toString()
}
getUserFolderData.launch("".toUri())
}
mytools.debug("userFolderData = ${userFolderData}")
EDIT 3
So I ran a test on the URI returned; always the same story not matter what I do, yes that's pretty ugly hack but it's the easiest way I can find to test it.
ivtitleimage.setImageURI("content://com.android.externalstorage.documents/document/primary%3ATest%2FMelsDeck%2Fbendover01.jpg".toUri())
31833-31833/com.example.cardgamexxx E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cardgamexxx, PID: 31833
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cardgamexxx/com.example.cardgamexxx.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{5a390cf 31833:com.example.cardgamexxx/u0a741} (pid=31833, uid=10741) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
EDIT 4
Right, so user selects folder/creates one with registerForActivityResult code above, I place files in it with Cx File Explorer and automagically drum roll! ...
I do not have read access to the file placed there, anyway thought I'd comment here as I don't want people stumbling on the post and thinking this works, because it doesn't.
On an Android 11 device your app can only create folders on root of external storage in the classic way if it has requested 'all files acces'.
Or it can use Storage Access Framework and for instance ACTION_OPEN_DOCUMENT_TREE to let the user create a folder in root of external storage and then select it.
Hi I recently downloaded the open source version of pdfjet that is offered on their website I extracted the downloaded zip and copied the java files found in the com\pdfjet folder to android studio next to my MainActivity.java. I tried a couple of examples that uses some of the corefonts and it works flawlessly but When I want to embed a custom font for the text it won't work on android studio.
I searched on the Internet for a solution but the only solution I found is this:
Font f2 = new Font(pdf,
new BufferedInputStream(getClass().getResourceAsStream(
"fonts/DroidFonts/DroidSerif-Italic.ttf")),
CodePage.UNICODE,
Embed.YES);
From:
http://pdfjet.com/java/examples/example-07.html
But android studio output an error
like in this image:
https://i.stack.imgur.com/W3SPG.png
Any possible solution?
Note: I am using the open source version and I want to use it in Android application
Thanks in advance :-)
They did change their constructor. Just remove "CodePage.UNICODE" part.
protected final Font getFont(String fontName, PDF thePdf) throws Exception {
InputStream fontStream = context.getAssets().open(TypefaceContainer.getFontPathFromName(fontName));
Font result = new Font(thePdf, fontStream, Embed.YES);
return result;
}
I'm currently using Appium and have hit an issue with regard to finding the WebView context. Using UIAutomator I can inspect the WebView and see its child elements, however I cannot access any of these by using CSS selector/ XPATH because I cannot switch to WebView.
I have tried looping over driver.getContextHandles(), however this simply returns "NAVTIVE_APP".
Android version being used is 5.0.1 and the latest version of Appium.
Does anyone know of a way to find out the name of the WebView or possibly being able to switch to it?
You need to handle the contexts....below is an example of handling Login with Facebook webview. It worked for me....
Set<String> contextHandles = driver.getContextHandles();
for (String s : contextHandles) {
System.out.println("Context : " + s);
//if context contains webview then set the webview context
if (s.contains("WEBVIEW")) {
//System.out.println("Set the Context NATIVE to WEBVIEW");
driver.context(s);
}
}
driver.findElement(By.xpath("//android.widget.EditText[#index='0']")).sendKeys(EMAIL);
//System.out.println("UserName Entered");
//sleep(3000);
driver.findElement(By.xpath("//android.widget.EditText[#index='1']")).sendKeys(PASSWORD);
//System.out.println("Password Entered");
I have a storage box admin page that's supposed to open with Java Web Start. However, on all browsers on my MacBook, this doesn't happen and instead I just get a html page saved with contents: "v6.3.1a Web Tools 10.1.18.222 ".
Looking at the javascript code of the page, I see it is trying to detect if correct Java Web Start is installed:
function webstartVersionCheck(versionString) {
// Mozilla may not recognize new plugins without this refresh
navigator.plugins.refresh(true);
// First, determine if Web Start is available
if **(navigator.mimeTypes['application/x-java-jnlp-file'])** {
// Next, check for appropriate version family
for (var i = 0; i < navigator.mimeTypes.length; ++i) {
pluginType = navigator.mimeTypes[i].type;
if (pluginType == "application/x-java-applet;version=" + versionString) {
return true;
}
}
}
return false;
}
Which is called here:
function writeMozillaData(page) {
versionCheck = webstartVersionCheck("1.5");
if (!versionCheck) {
var pluginPage = "http://jdl.sun.com/webapps/getjava/BrowserRedirect?locale=en&host=java.com";
document.write("The version of Java plugin needed to run the application is not installed. The page from where the plugin can be downloaded will be opened in a new window. If not, please click here: Download correct Java version.");
window.open(pluginPage, "needdownload");
} else {
window.location = page;
}
}
I put in an alert in the mimeTypes and notice that there is no mimeType of 'application/x-java-jnlp-file' which shows up in navigator.
Questions:
Is this what is causing the browser to interpret the content as just text/html and save the html?
How can I force the launch of Java Web Start here?
I do have firefox settings indicating that jnlp get handled by Java Web Start application, hence I suspect the browser is not interpreting the page as jnlp at all to begin with.
..there is no mimeType of application/x-java-jnlp-file which shows up in navigator.
Is this what is causing the browser to interpret the content as just text/html and save the html?
Almost certainly yes. Fix the content-type.