Android:如何从TextView链接本地HTML文件

时间:2014-10-31 04:56:47

标签: android html android-intent textview activitynotfoundexception

我知道这里有很多关于阅读本地HTML文件的问题,但我找不到满足我需求的任何问题。

我想弹出一个DialogFragment,显示一条带有链接的消息,点击该链接并读取一个本地HTML文件(最好是从默认的Web浏览器)。

到目前为止我所拥有的:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
        alertDialogBuilder.setMessage(Html.fromHtml("I have read and agree to the " + "<a href=\"file:///android_asset/privacy.html\">Terms and Conditions</a>" + " and the " + "<a href=\"http://www.samsung.com/global/business/mobile/info/privacy.html\">Privacy Policy</a>"));
        alertDialogBuilder.setCancelable(false);

        //rest of code...
    }

指向在线版本的第二个链接就像魅力一样。第一个链接应指向我的assets文件夹中的本地privacy.html文件。单击第一个链接时,会发生错误:

    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///android_asset/privacy.html (has extras) }

对于这种情况,任何人都有任何提示/建议吗?我尝试了多个不同的HTML文件,都具有相同的结果。我是否必须使用自己的WebView来做这样的事情?似乎应该有一种方法可以让Android在点击链接时从默认浏览器中查看本地HTML文件......

2 个答案:

答案 0 :(得分:3)

从技术上讲,它不起作用,因为Link是html标签。它将在webview中工作。你还想用textview做这个,你已经自定义了你的代码 试试这段代码:

public class CustomLinkMovementMethod extends LinkMovementMethod
{

private static Context movementContext;

private static CustomLinkMovementMethod linkMovementMethod = new CustomLinkMovementMethod();

public boolean onTouchEvent(android.widget.TextView widget, android.text.Spannable buffer, android.view.MotionEvent event)
{
    int action = event.getAction();

    if (action == MotionEvent.ACTION_UP)
    {
        int x = (int) event.getX();
        int y = (int) event.getY();

        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();

        x += widget.getScrollX();
        y += widget.getScrollY();

        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
        if (link.length != 0)
        {
            String url = link[0].getURL();
            if (url.startsWith("https"))
            {
                Log.d("Link", url);
                Toast.makeText(movementContext, "Link was clicked", Toast.LENGTH_LONG).show();
            } else if (url.startsWith("tel"))
            {
                Log.d("Link", url);
                Toast.makeText(movementContext, "Tel was clicked", Toast.LENGTH_LONG).show();
            } else if (url.startsWith("mailto"))
            {
                Log.d("Link", url);
                Toast.makeText(movementContext, "Mail link was clicked", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    }

    return super.onTouchEvent(widget, buffer, event);
}

public static android.text.method.MovementMethod getInstance(Context c)
{
    movementContext = c;
    return linkMovementMethod;
}

应该以下列方式从textview调用:

textViewObject.setMovementMethod(CustomLinkMovementMethod.getInstance(context));

答案 1 :(得分:1)

myTextView.setText(Html.fromHtml(readTxt())); 

//此函数将返回您可以在textview中设置的字符串。并且该String具有html代码,因此请使用Html.fromHtml

 private String readTxt(){
 InputStream inputStream = getResources().openRawResource(R.raw.My_html_file);
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 int i;
 try {
 i = inputStream.read();
  while (i != -1)
  {
   byteArrayOutputStream.write(i);
   i = inputStream.read();
  }
  inputStream.close();
  } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
  return byteArrayOutputStream.toString();
}
 }

这个已经是answered