如何通过电子邮件发送webview内容正文?

时间:2015-04-14 18:32:18

标签: android android-intent android-fragments android-activity android-webview

目前在我的应用程序中,我正在使用webview来显示我的html内容(这是通过资源文件夹按预期进行,只是为了澄清)。但是,我已经通过电子邮件功能集成了发送功能,我将主题正文和标题视为空,而不是选择的所选电子邮件中的焦点内容。 (比如当我通过电子邮件选项选择我的发送时选择gmail。我看到内容正文为null而不是webview中的内容)。任何人都做过这个或有想法,如何去做同样的事情? 这是我的代码: 我的EmailUtils课程:

public class EmailUtils {
    public static String FEEDBACK_EMAIL = "Android.Feedback@mycompany.com";

    public static void shareNewsViaEmail(final Fragment fragment, final String emailSubject, final String emailBody){
        shareNewsViaEmailEx(fragment.getActivity(), emailSubject, emailBody);
    }

    public static void shareNewsViaEmailEx(final Context context, final String emailSubject, final String emailBody){
        final Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("text/html");
        final String subjectAppendText = " " + context.getResources().getString(R.string.email_subject_append);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject + subjectAppendText);
        emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
        context.startActivity(Intent.createChooser(emailIntent, "Send Email"));
    }

}

Part My Fragment类:

//The button where it's called:
@Override
    public void onMenuItemClicked(final int position) {
        mOptionsMenuHelper.hideMenu();
        menuButton.setSelected(false);
        switch (position) {

        case OptionMenuItems.EMAIL_STORY_POSITION:
            emailArticle();
            break;
        }
    }

private void emailArticle() {
        final Fragment fragment =  mArticleAdapter.getFragment(mArticlePager.getCurrentItem());

        if (fragment instanceof ArticleFragmentWebView)  {
            final String emailSubject = ((ArticleFragmentWebView)fragment).getHeadline();
            final String articleBody  = ((ArticleFragmentWebView)fragment).getArticleBody();
            final String newLine = getResources().getString(R.string.new_line);
            final String disclamer = getResources().getString(R.string.intellectual_property_info);
            final String emailBody = articleBody + newLine + disclamer;
            EmailUtils.shareNewsViaEmail(this, emailSubject, emailBody);
        }
    }

我该怎么做?

谢谢! 这是articlefragmentwebview:

public class ArticleFragmentWebView extends AbsBaseArticleFragment {
    public static final String TAG_ARTICLE_FRAGMENT = "ArticleFragment";
    public static final String ARTICLE_POSITION = "article_position";
    public static final String CATEGORY_CODE = "article_code";
    private int mPositionInPager;
    private ArticleWebViewClient articleWebViewClient;

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mPositionInPager = getArguments().getInt(ARTICLE_POSITION);
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        final View view = super.onCreateView(inflater, container, savedInstanceState);
        articleWebViewClient = new TechnicArticleWebViewClient(getActivity(), view);
        return view;
    }

    @Override
    public void onLoading() {
            final View fragmentView = getView();
            if (fragmentView != null) {
                LayoutUtils.showLoading(fragmentView, "");
            }
    }   
    @Override
    public void showLoading(View view) {
            LayoutUtils.showLoading(view, "");
    }
    @Override
    public void showResults(final Uri uri) {
            LayoutUtils.showResults(getView(), R.id.news_body);
    }   
    @Override
    public void setTextSize(TextSize textSize) {
        articleWebViewClient.increaseFontSize();
    }
    @Override
    public void showNoResults(final Uri uri) {
    //  LayoutUtils.showNoResult(getView(), R.id.details_container);
    }
    @Override
    public void showRelatedDivider() {

    }
    @Override
    protected void getRelatedQuotes() {

    }
    @Override
    public Uri onCreateContentUri() {
        final String articleCode = getArguments().getString(ARTICLE_CODE);
        return Uri.withAppendedPath(NewsContentProvider.ARTICLE_MYNEWS_URI, articleCode);
    }




    @Override
    public void onStart() {
        super.onStart();
    }

    public int getPositionInPager() {
        return mPositionInPager;
    }

    @Override
    protected int getFragmentLayoutId() {
        return R.layout.fragment_article_briefcase;
    }

    @Override
    protected int getArticleTextViewId() {
        return R.id.news_body;
    }

    @Override
    protected int getFlowTextViewId() {
        return R.id.tv;
    }

    @Override
    protected int getHeaderTextViewId() {
        return R.id.headline;
    }

    @Override
    protected int getMetadataTextViewId() {
        return R.id.timestamp_and_source;
    }

    @Override
    public void onMenuItemClicked(final int position) {
        //Do nothing
    }
    @Override
    public boolean setViewValue(final View view, final Cursor cursor, final int columnIndex) {
        if (view.getId() !=  R.id.news_body)            // paranoia
            return true;

        String article = cursor.getString(columnIndex);
        String storyId = cursor.getString(cursor.getColumnIndex(Briefcase.Columns.ID));
        return articleWebViewClient.setArticle(article, storyId);
    }
}

这里是getarticlebody的模型类:

public class Briefcase {


        public static final String TEXT_TYPE = "TEXT";
        public static final String HTML_TYPE = "Html";
        public static final String PDF_TYPE = "PDF";

    public static class Columns {

        public static final String ID = "Id";
        public static final String NAME = "Name";
        public static final String SAVED_DATE = "SavedDate";
        public static final String TYPE = "DocumentType";
        public static final String DOCUMENT_DATE = "DocumentDate";
        public static final String SOURCE = "DocumentSource";
        public static final String DOCUMENT_LINK = "DocumentLink";
        public static final String DOCUMENT_ID = "DocumentId";
        public static final String IS_READ = "IsRead";
        public static final String ARTICLE_BODY = "ArticleBody";
        //PrimaryRic
    }

    public Briefcase(String id, String articleId, String name, String docDate, String source, String docType){
        mId = id;
        mDocumentId = articleId;
        mName = name;
        mPNACDate = docDate;
        mDocumentSource = source;
        mDocumentType = docType;
        mIsRead = "false";
        mSavedDate = new Date();
    }

    @SerializedName(Columns.ID)
    private String mId;

    @SerializedName(Columns.NAME)
    private String mName;

    @SerializedName(Columns.SAVED_DATE)
    private Date mSavedDate;

    @SerializedName(Columns.TYPE)
    private String mDocumentType;

    @SerializedName(Columns.DOCUMENT_DATE)
    private String mPNACDate;

    @SerializedName(Columns.SOURCE)
    private String mDocumentSource;

    @SerializedName(Columns.DOCUMENT_ID)
    private String mDocumentId;

    @SerializedName(Columns.IS_READ)
    private String mIsRead;

    public String getId() {
        return mId;
    }

    public String getName() {
        return mName;
    }

    public Date getSavedDate() {
        return mSavedDate;
    }

    public String getDocumentType() {
        return mDocumentType;
    }

    public String getPNACDate() {
        return mPNACDate;
    }

    public String getDocumentSource() {
        return mDocumentSource;
    }

    public String getDocumentLink() {
        String docLink = null;
        if(StringUtils.isEmpty(mDocumentSource))
            return docLink;

        return mDocumentId;

    }

    public String getIsRead() {
        return mIsRead;
    }

    public ContentValues toContentValues() {
        if (hasValidData()) {
            final ContentValues values = new ContentValues();
            values.put(Columns.ID, mId);
            values.put(Columns.NAME, decodeHeadline());
            values.put(Columns.SAVED_DATE, mSavedDate.getTime());
            values.put(Columns.DOCUMENT_LINK, getDocumentLink());
            values.put(Columns.DOCUMENT_DATE, DateUtils.convertFromUTCStringToLong(mPNACDate));
            values.put(Columns.SOURCE, mDocumentSource);
            values.put(Columns.IS_READ, mIsRead);
            values.put(Columns.TYPE, mDocumentType);
            values.put(GenericColumns.USER_ID, SharedPreferencesManager.getInstance().getLoginUsername());
            return values;
        }
        return null;
    }

    private String decodeHeadline() {
        String decodedHeadline = mName;
        try {
            decodedHeadline = URLDecoder.decode(mName, NetworkUtils.Values.UTF_8);
        } catch (final Exception e) {
            e.printStackTrace();
        }
        return decodedHeadline;
    }

    public ContentValues toContentForDatabaseUpdateValues() {
        if (hasValidData()) {
            final ContentValues values = toContentValues();
            values.put(Columns.ARTICLE_BODY, getArticleBody());
            return values;
        }
        return null;
    }

    private String getArticleBody() {
        String body = "";
        final ContentResolver resolver = Application.getAppContext().getContentResolver();
        final String[] projection = new String [] {Columns.ARTICLE_BODY};
        final Uri uri = Uri.withAppendedPath(BriefcaseContentProvider.BRIEFCASE_ARTICLE_STORY_URI, mId);
        final Cursor cursor = resolver.query(uri, projection, null, null, null);
        if(cursor.getCount()>0){
            cursor.moveToFirst();
            body = cursor.getString(cursor.getColumnIndex(Columns.ARTICLE_BODY));
        }
        return body;
    }

    private boolean hasValidData() {
        boolean isValid = false;
        if (StringUtils.isNotEmpty(mId) && StringUtils.isNotEmpty(mName)){
            isValid = true;
        }
        return isValid;
    }

}

1 个答案:

答案 0 :(得分:3)

让你尝试过Html.fromHtml(字符串)。下面的代码非常适合在我的电子邮件中显示html

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String body = convertStreamToString(getResources().openRawResource(R.raw.share_email_body));
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "my subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));
startActivity(Intent.createChooser(emailIntent, "Email:"));