0
我創建了一個顯示多個網頁的應用程序。我希望該應用能夠與WhatsApp和其他即時通訊工具共享該網頁上顯示的圖像,因爲我添加了一個上下文菜單並已實施ACTION_SEND,但無法正常工作。當我試圖共享圖像它給我的錯誤「分享失敗請重試」這是我的代碼將圖像從webview共享到whatsapp
public class TopRatedFragment extends Fragment {
private ProgressBar progress;
private WebView myWebView2;
private Menu optionsMenu;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
String url = "http://images.google.com";
myWebView2 = (WebView) rootView.findViewById(R.id.webViewTop);
myWebView2.setWebChromeClient(new myWebViewClient());
myWebView2.getSettings().setJavaScriptEnabled(true);
progress = (ProgressBar) rootView.findViewById(R.id.progressBar3);
progress.setMax(100);
setHasOptionsMenu(true);
myWebView2.loadUrl(url);
myWebView2.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView View, String url) {
View.loadUrl(url);
TopRatedFragment.this.progress.setProgress(0);
return true;
}
});
myWebView2.setOnKeyListener(new android.view.View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
return rootView;
}
@Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
optionsMenu = menu;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.airport_menuRefresh:
TopRatedFragment.this.myWebView2.reload();
setRefreshActionButtonState(true);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, v.getId(), 0, "Call");
menu.add(0, v.getId(), 0, "share image");
}
@Override
public boolean onContextItemSelected(MenuItem item){
if(item.getTitle()=="Call"){
Toast.makeText(getActivity(), "calling code", Toast.LENGTH_LONG).show();
}
else if(item.getTitle()=="share image")
{
// This is the code which i am using for share intent
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageState()));
startActivity(Intent.createChooser(share, "Share image using"));
}else{
return false;
}
return true;
}
感謝您的幫助!
你能舉個例子嗎?我如何在一個網頁上添加圖片的uri。 – Rahul
在web視圖中打開圖像還是包含打開圖像的html文檔?我想在這種情況下,你必須解析img元素的HTML並獲取圖像的src。 – JHH
它在webview中打開,而不是在html文檔中打開。我怎樣才能做到這一點? – Rahul