我正在尝试设置一个应用程序以在Webview中加载URL,然后让应用程序能够将任何单击的图像设置为壁纸。
到目前为止,我可以加载网址并显示图库。
我非常关注如何将可点击图像设置为壁纸。
任何帮助都将不胜感激。
我的代码:
package com.dg.rWallpapers;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class rWallpapersActivity extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
/* mWebView.getSettings().setBuiltInZoomControls(true); */ //Removed I do not want zooming.
mWebView.getSettings().setUseWideViewPort(true);
mWebView.loadUrl("http://wall.dg.net?wallId=5");
mWebView.setWebViewClient(new HelloWebViewClient(this));
// Lookup R.layout.main
RelativeLayout layout = (RelativeLayout)findViewById(R.id.ad);
AdView adView = new AdView(this, AdSize.BANNER, "xxxx");
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
//request.setTesting(true);
adView.loadAd(request);
}
public class HelloWebViewClient extends WebViewClient {
private WallpaperManager mWallpaperManager;
private Context mContext;
public HelloWebViewClient(Context context) {
mContext = context;
mWallpaperManager = WallpaperManager.getInstance(context);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".png") || url.endsWith(".jpg")) {
try {
Bitmap b = downloadImage(url);
mWallpaperManager.setBitmap(b);
Toast.makeText(mContext, "Done!!", Toast.LENGTH_SHORT).show();
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
private Bitmap downloadImage(String fileUrl) throws IOException {
try {
HttpGet httpRequest = new HttpGet(new URL(fileUrl).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
BufferedHttpEntity buffer = new BufferedHttpEntity(response.getEntity());
return BitmapFactory.decodeStream(buffer.getContent());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:3)
您可以使用自定义WebViewClient
并实施shouldOverrideUrlLoading()
方法来处理活动中图片的点击。在那里,您可以下载图像并将其设置为壁纸。
以下是此类WebViewClient
的示例:
public class MyWebViewClient extends WebViewClient {
private WallpaperManager mWallpaperManager;
private Context mContext;
public MyWebViewClient(Context context) {
mContext = context;
mWallpaperManager = WallpaperManager.getInstance(context);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".png") || url.endsWith(".jpg") || url.endsWith(".jpeg")) {
new AsyncTask<String, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(String... params) {
try {
return downloadImage(params[0]);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
try {
mWallpaperManager.setBitmap(result);
Toast.makeText(mContext, "Done!!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.execute(url);
return true;
}
return false;
}
private Bitmap downloadImage(String fileUrl) throws IOException {
try {
HttpGet httpRequest = new HttpGet(new URL(fileUrl).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
BufferedHttpEntity buffer = new BufferedHttpEntity(response.getEntity());
return BitmapFactory.decodeStream(buffer.getContent());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
}
您可以通过活动中的setWebClient(new MyWebViewClient(this))
将WebViewClient添加到WebView中。