我创建了一个显示多个网页的应用。我希望该应用程序与whatsapp和其他IM信使分享在该网页上显示的图像,因为我已经添加了一个上下文菜单并已实施ACTION_SEND但它没有工作。当我尝试分享图片时,它会给我错误"分享失败请再试一次" 这是我的代码
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;
}
感谢您的帮助!
答案 0 :(得分:0)
这条线看起来很奇怪
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageState()));
stream extra应该包含你想要分享的图像的uri,但是你将它设置为我甚至不理解它应该做什么的东西 - 甚至不能评估为uri。 目前,您不会向意图提供有关您要发送的内容的任何信息。将流额外设置为图像位置uri,你应该没问题。
(顺便说一句,你真的应该使用equals()而不是==来比较字符串。你不应该识别标题上的菜单项,因为它在本地化时会失败等。使用菜单项ID。)