Home > Mobile >  Js read Android local files
Js read Android local files

Time:09-23

Recent projects in android WebView or XWalkView applied loads a large 3 d model (which is actually use threejs method loading), have controlled M big, I tried many kinds of methods:
1. In the assets folder, packaged into the apk, as assets folder with a single file size limit, and file assembly error,
(2) on the Android emulator local (I put in storage/emulated/0/DownLoad folder), but the file path in the js don't know what to write,

The problem is to summarize:
(because of the need to write in js) storage/emulated android simulator/0/DownLoad/model. The obj is the absolute path?
Or write in js relative path method is what?

I tried this a few absolute path, wrong:
"/storage/emulated/0/DownLoad/model. The obj"
"/DownLoad/model. Obj"

CodePudding user response:

Should interact with the android source code to get, should not directly access the sd card file below

CodePudding user response:

Android code to declare the corresponding JavaScriptInterface method, this method for js invoked,
Then you can be your next step in this method:
Environment. External.getexternalstoragedirectory () - & gt; This is the path of the access to external storage (because of different devices may be slightly different path, so use this method can obtain the corresponding storage directory, then stitching on your folder, file name)

CodePudding user response:

Stick a top posts

CodePudding user response:

refer to the second floor ganshenml response:
Android code to declare the corresponding JavaScriptInterface method, this method for js invoked,
Then you can be your next step in this method:
Environment. External.getexternalstoragedirectory () - & gt; This is the path of the access to external storage (because of different devices may be slightly different path, so use this method can obtain the corresponding storage directory, then stitching on your folder and file name)


Environment. External.getexternalstoragedirectory () + file path, the string passed to js, js is unable to find a file, as 1/f, puts it,

reference 1st floor jscoolstar response:
should interact with the android source code to get, should not directly access the sd card file below


But if use Java code to load the file directly into the cache, then the loaded into the data such as assignment to a variable, and I eventually is to analysis in js, processing and display of the model data, but how can this variable to js?

Feasible method is actually two kinds:
One is that load the data in the js (seems can't find the android local files, Environment. External.getexternalstoragedirectory () + also useless file path);
Second, load data in Java (as in variable mass less than js),

What to do??
I'm party!!!!!

CodePudding user response:

reference 4 floor qq_33510958 response:
Quote: refer to the second floor ganshenml response:

Android code to declare the corresponding JavaScriptInterface method, this method for js invoked,
Then you can be your next step in this method:
Environment. External.getexternalstoragedirectory () - & gt; This is the path of the access to external storage (because of different devices may be slightly different path, so use this method can obtain the corresponding storage directory, then stitching on your folder and file name)


Environment. External.getexternalstoragedirectory () + file path, the string passed to js, js is unable to find a file, as 1/f, puts it,

reference 1st floor jscoolstar response:
should interact with the android source code to get, should not directly access the sd card file below


But if use Java code to load the file directly into the cache, then the loaded into the data such as assignment to a variable, and I eventually is to analysis in js, processing and display of the model data, but how can this variable to js?

Feasible method is actually two kinds:
One is that load the data in the js (seems can't find the android local files, Environment. External.getexternalstoragedirectory () + also useless file path);
Second, load data in Java (as in variable mass less than js),

What to do??
I'm party!!!!!

You through the phone first to check whether to download the file to the local, and then write this file to obtain a death

CodePudding user response:

reference 5 floor ganshenml reply:
Quote: refer to 4th floor qq_33510958 response:

Quote: refer to the second floor ganshenml response:

Android code to declare the corresponding JavaScriptInterface method, this method for js invoked,
Then you can be your next step in this method:
Environment. External.getexternalstoragedirectory () - & gt; This is the path of the access to external storage (because of different devices may be slightly different path, so use this method can obtain the corresponding storage directory, then stitching on your folder and file name)


Environment. External.getexternalstoragedirectory () + file path, the string passed to js, js is unable to find a file, as 1/f, puts it,

reference 1st floor jscoolstar response:
should interact with the android source code to get, should not directly access the sd card file below


But if use Java code to load the file directly into the cache, then the loaded into the data such as assignment to a variable, and I eventually is to analysis in js, processing and display of the model data, but how can this variable to js?

Feasible method is actually two kinds:
One is that load the data in the js (seems can't find the android local files, Environment. External.getexternalstoragedirectory () + also useless file path);
Second, load data in Java (as in variable mass less than js),

What to do??
I'm party!!!!!

You through the phone first to check whether to download the file to the local, and write this file to get the dead



Mobile phone has a file on the key is how to embedded h5 access file? I also encountered this problem, the h5 path should be how to write?

CodePudding user response:

Webview access to the file is usually rewrite MyWebChromeClient openFileChooser (4.4 and 4.4.3, not Override) and onShowFileChooser (5.0 and later)



OpenFileChooser returned directly file path can
OnShowFileChooser need to convert files to the media data to be used, here are the images path into media play code
 
Public static Uri getMediaContentUri (String absolutePath) {
Uri newUri;
//to find whether there is this uri
Cursor Cursor=MyLibsApp. MApp. GetContentResolver () query (
MediaStore. Images. Media EXTERNAL_CONTENT_URI,
New String [] {MediaStore. Images. Media. _ID},
MediaStore. Images. Media. The DATA + "=? "
New String [] {absolutePath}, null);
If (cursor!=null & amp; & Cursor. MoveToFirst ()) {
Int id=cursor. Get int (cursor. GetColumnIndex (MediaStore. MediaColumns. _ID));
NewUri=Uri. WithAppendedPath (MediaStore. Images. Media. EXTERNAL_CONTENT_URI, String, the valueOf (id));
} else {
ContentValues values=new ContentValues ();
Values. The put (MediaStore. Images. Media. The DATA, absolutePath);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related