Android Webview H5 resource localization

Android Webview H5 resource localization

A. Create a separate module to read resource project

1. Project dependent benefits

In line with the idea of ​​modular, they are independent of each other. A project holds a reference to another project, modifying more convenient.

(Note: compile project compile the contents of the referenced project must contain java code, xml layout files, AndroidManifest, etc., but also include a reference in the form of a statement by the setting.gradle project)

2. Procedure project import ProjectR

The project is dependent ProjectR does not require any changes!

1. In the project requires the use of the settings.gradleAdd Configuration

include ':ProjectR'
project(':ProjectR').projectDir = new File(settingsDir,'../../ProjectR/') include ':ProjectR:app'

2. In the project requires the use of the Moduleadded library need to be introduced

dependencies {
    ...
    compile project(path: ':ProjectR:app')
    ...
}

3. Set the correct path is dependent on the project

project(':BProject').projectDir = new File(settingsDir,'../../ProjectR/')

Where the  new File(settingsDir,'../../ProjectR/')
parameters:

参数一: settingsDir 指的是相对于 settings.gradle 文件所在路径
参数二: 填写被依赖项目的路径,**../**表示上级目录,所以根据自己的路径修改

3. Pit (Note)

If you are not careful an erroneous path is dependent on the project, but also point the synchronization project. It may be generated in the Project and similar xxx_xxx.iml Module directory file, if the file exists abnormal, even if your path back may also be configured correctly synchronized unsuccessful, continue to prompt an error. This is you only need to delete the file synchronization item to Syria.

 

4. Assets resource file read and AssetManager

  AssetManager manage access to assets folder resources, it allows you to form a simple byte stream open the original file and read resources and applications bundled together. The main use list () and open () method.

  finalString [] list (Stringpath) returns all file and directory names in the specified path, path is a relative path, are assets subdirectory.
  finalInputStream open (String fileName) using ACCESS_STREAMING mode on the specified file in the assets, fileName is a relative path, it is assets sub.
  finalInputStream open (String fileName, int accessMode ) access mode using the display to open the specified file in the assets.

5. ProjectR project the main code

import android.app.Activity;
import android.content.res.AssetManager;
import android.webkit.MimeTypeMap;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;

import java.io.FileNotFoundException;
import java.io.InputStream;

public  class LocalAssets {

    private static LocalAssets _instance = null;

    public static LocalAssets getInstance() {
        return _instance != null ? _instance : (_instance = new LocalAssets());
    }

    private Activity mainContext = null;
    private String resUrl = "";

    /**
     * Initializes get context
     */
    public boolean init(Activity context) {
        mainContext = context;
        return true;
    }

    /** 
     * Block webview request address into read local files
     */
    public WebResourceResponse doLoadRes(WebView view, String url) {
        try {
       // intercept response resource replacement according to the local resources are judged to be loaded url IF (0 ) {
                AssetManager assetManager = mainContext.getAssets();
                LocUrl String = "resource path after the assets";
                InputStream stream = assetManager.open(locUrl);
                WebResourceResponse response = new WebResourceResponse(
                        MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url))
                        , "UTF-8", stream
                );
                return response;
            }
        } catch (FileNotFoundException e) {
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return null;
    }

}

 

 

 

II. Main project code

1. The need for localized resources into assets folder.

 

2. Set Library After initialization webview introduced:

LocalAssets.getInstance().init(this);

 

3. Set webview intercepts the request:

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
  WebResourceResponse resp = LocalAssets.getInstance().doLoadRes(view, url);
  if (resp != null) {
    return resp;
  }
  return super.shouldInterceptRequest(view, url);
}

@Override
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
  WebResourceResponse resp = LocaalAssets.getInstance().doLoadRes(view, request.getUrl().toString());
  if (resp != null) {
    return resp;
  }
  return super.shouldInterceptRequest(view, request);
}

 

Guess you like

Origin www.cnblogs.com/dlm17/p/12344302.html