如果您对AndroidWebView下载图像的路径为“缓存”感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于AndroidWebView下载图像的路径为“缓存”的详细内容,我
如果您对Android WebView下载图像的路径为“缓存”感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Android WebView下载图像的路径为“缓存”的详细内容,我们还将为您解答android webview下载文件的相关问题,并且为您提供关于android listview的item中有webview,但是点击webview会没有反应?、Android ViewFlipper滑动切换webview,webview里的网页布局变得混乱、android webview js 交互,响应 webview 图片的响应事件、android webview js交互, 响应webview中的图片点击事件的有价值信息。
本文目录一览:- Android WebView下载图像的路径为“缓存”(android webview下载文件)
- android listview的item中有webview,但是点击webview会没有反应?
- Android ViewFlipper滑动切换webview,webview里的网页布局变得混乱
- android webview js 交互,响应 webview 图片的响应事件
- android webview js交互, 响应webview中的图片点击事件
Android WebView下载图像的路径为“缓存”(android webview下载文件)
我正在尝试使用WebView在Android中显示我的博客文章.
我可以将HTML文本保存在sqlite DateBase中,以供用户离线时使用,但问题是用户在线时将图像保存在目录中.
下次访问时,如果在指定目录中找到图像,请使用它! ,否则下载并缓存以备将来使用!
解决方法:
尝试启用WebView应用程序缓存:
// Set cache size to 8 mb by default. should be more than enough
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);
mWebView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
mWebView.getSettings().setAppCacheEnabled(true);
cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
if (cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
您将需要添加.ACCESS_NETWORK_STATE和.ACCESS_WIFI_STATE权限.
Here,您可以找到有关所有WebSettings的更多信息.
android listview的item中有webview,但是点击webview会没有反应?
listview的item中嵌套有webview,给listview设置了onclickItem的点击事件,但是当点到webview的位置时没有响应,其他位置却可以,应该怎么设置才能在点到webview的位置时也有响应?Android ViewFlipper滑动切换webview,webview里的网页布局变得混乱
@红猎人 你好,想跟你请教个问题:师兄啊急求一个问题。
网页是自己写的,用div又嵌套了几个网页。若比较快的滑到该屏不出现该问题,若等一会儿滑到该屏就有此问题,请问该怎么解决。
android webview js 交互,响应 webview 图片的响应事件
方案思路,
1. 在点击图片的时候调用本地的 java 方法并给出响应的图片地址
2. 本地获得图片地址后,开启一个遮罩 activity 进行显示和处理
第二步的实现很容易实现,关键是第一步的实现,在网页中点击图片不会调用本地的 java 代码。那么我们需要给这个点击事件加上相应的 js 函数,让点击事件调用的 js 函数来调用我们提前准备好的 java 函数,等我们捕获到图片的 url 剩下的就好处理了。
关键点就是给普通的 html 注入我们的 js 函数,让图片能够响应点击并调用 js 函数,在通过 js 函数来调用我们的 java 函数。听起来好像有点绕,不过也不难,下面我们用代码实现下
对 java 和 js 交互还不熟悉的同学,请参照前面的文章
http://blog.csdn.net/wangtingshuai/article/details/8631835
这次实例的主要功能:点击图片在新的 activity 中展示,对图片能够进行手势操作,包括双指缩放等
效果图
加载 webview 的 activity 代码
[java] view plaincopy
package wst.webview;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
private WebView contentWebView = null;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contentWebView = (WebView) findViewById(R.id.webview);
// 启用 javascript
contentWebView.getSettings().setJavaScriptEnabled(true);
// 随便找了个带图片的网站
contentWebView.loadUrl("http://www.weim.me/12408.html");
// 添加 js 交互接口类,并起别名 imagelistner
contentWebView.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");
contentWebView.setWebViewClient(new MyWebViewClient());
}
// 注入 js 函数监听
private void addImageClickListner() {
// 这段 js 函数的功能就是,遍历所有的 img 几点,并添加 onclick 函数,函数的功能是在图片点击的时候调用本地 java 接口并传递 url 过去
contentWebView.loadUrl("javascript:(function(){" +
"var objs = document.getElementsByTagName(\"img\"); " +
"for(var i=0;i<objs.length;i++) " +
"{"
+ " objs[i].onclick=function() " +
" { "
+ " window.imagelistner.openImage(this.src); " +
" } " +
"}" +
"})()");
}
// js 通信接口
public class JavascriptInterface {
private Context context;
public JavascriptInterface(Context context) {
this.context = context;
}
public void openImage(String img) {
System.out.println(img);
//
Intent intent = new Intent();
intent.putExtra("image", img);
intent.setClass(context, ShowWebImageActivity.class);
context.startActivity(intent);
System.out.println(img);
}
}
// 监听
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
view.getSettings().setJavaScriptEnabled(true);
super.onPageFinished(view, url);
// html 加载完成之后,添加监听图片的点击 js 函数
addImageClickListner();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.getSettings().setJavaScriptEnabled(true);
super.onPageStarted(view, url, favicon);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
}
展示图片的 activity 代码
[java] view plaincopy
package wst.webview;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;
public class ShowWebImageActivity extends Activity {
private TextView imageTextView = null;
private String imagePath = null;
private ZoomableImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_webimage);
this.imagePath = getIntent().getStringExtra("image");
this.imageTextView = (TextView) findViewById(R.id.show_webimage_imagepath_textview);
imageTextView.setText(this.imagePath);
imageView = (ZoomableImageView) findViewById(R.id.show_webimage_imageview);
try {
imageView.setImageBitmap(((BitmapDrawable) ShowWebImageActivity.loadImageFromUrl(this.imagePath)).getBitmap());
} catch (IOException e) {
e.printStackTrace();
}
}
public static Drawable loadImageFromUrl(String url) throws IOException {
URL m = new URL(url);
InputStream i = (InputStream) m.getContent();
Drawable d = Drawable.createFromStream(i, "src");
return d;
}
}
图片布局文件
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- TODO 默认占位图 -->
<wst.webview.ZoomableImageView
android:id="@+id/show_webimage_imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:src="@drawable/icon" />
<TextView
android:id="@+id/show_webimage_imagepath_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffff0000" />
</LinearLayout>
android webview js交互, 响应webview中的图片点击事件

package wst.webview;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
private WebView contentWebView = null;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contentWebView = (WebView) findViewById(R.id.webview);
// 启用javascript
contentWebView.getSettings().setJavaScriptEnabled(true);
// 随便找了个带图片的网站
contentWebView.loadUrl("http://www.weim.me/12408.html");
// 添加js交互接口类,并起别名 imagelistner
contentWebView.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");
contentWebView.setWebViewClient(new MyWebViewClient());
}
// 注入js函数监听
private void addImageClickListner() {
// 这段js函数的功能就是,遍历所有的img几点,并添加onclick函数,函数的功能是在图片点击的时候调用本地java接口并传递url过去
contentWebView.loadUrl("javascript:(function(){" +
"var objs = document.getElementsByTagName(\"img\"); " +
"for(var i=0;i<objs.length;i++) " +
"{"
+ " objs[i].onclick=function() " +
" { "
+ " window.imagelistner.openImage(this.src); " +
" } " +
"}" +
"})()");
}
// js通信接口
public class JavascriptInterface {
private Context context;
public JavascriptInterface(Context context) {
this.context = context;
}
public void openImage(String img) {
System.out.println(img);
//
Intent intent = new Intent();
intent.putExtra("image", img);
intent.setClass(context, ShowWebImageActivity.class);
context.startActivity(intent);
System.out.println(img);
}
}
// 监听
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
view.getSettings().setJavaScriptEnabled(true);
super.onPageFinished(view, url);
// html加载完成之后,添加监听图片的点击js函数
addImageClickListner();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.getSettings().setJavaScriptEnabled(true);
super.onPageStarted(view, url, favicon);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
}
展示图片的activity代码
package wst.webview;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;
public class ShowWebImageActivity extends Activity {
private TextView imageTextView = null;
private String imagePath = null;
private ZoomableImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_webimage);
this.imagePath = getIntent().getStringExtra("image");
this.imageTextView = (TextView) findViewById(R.id.show_webimage_imagepath_textview);
imageTextView.setText(this.imagePath);
imageView = (ZoomableImageView) findViewById(R.id.show_webimage_imageview);
try {
imageView.setImageBitmap(((BitmapDrawable) ShowWebImageActivity.loadImageFromUrl(this.imagePath)).getBitmap());
} catch (IOException e) {
e.printStackTrace();
}
}
public static Drawable loadImageFromUrl(String url) throws IOException {
URL m = new URL(url);
InputStream i = (InputStream) m.getContent();
Drawable d = Drawable.createFromStream(i, "src");
return d;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- TODO 默认占位图 -->
<wst.webview.ZoomableImageView
android:id="@+id/show_webimage_imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:src="@drawable/icon" />
<TextView
android:id="@+id/show_webimage_imagepath_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffff0000" />
</LinearLayout>
希望对大家有所帮助
关于Android WebView下载图像的路径为“缓存”和android webview下载文件的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于android listview的item中有webview,但是点击webview会没有反应?、Android ViewFlipper滑动切换webview,webview里的网页布局变得混乱、android webview js 交互,响应 webview 图片的响应事件、android webview js交互, 响应webview中的图片点击事件的相关信息,请在本站寻找。
本文标签: