使用getPrimaryClip()复制数据给定{text/plain {NULL}} [英] Copying data giving using getPrimaryClip() giving { text/plain {NULL} }
问题描述
使用ClipData
时得到{ text/plain {NULL} }
,但是如果使用不推荐使用的方法mClipboard.getText()
,则效果很好.
I am getting { text/plain {NULL} }
when I am using ClipData
but if I use deprecated method mClipboard.getText()
it is working just fine.
if (mClipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
ClipData clipData = mClipboard.getPrimaryClip();
ClipData.Item item = clipData.getItemAt(0);
Log.d(TAG, clipData.toString());
Log.d(TAG, mClipboard.getText());
}
更新
问题存在于三星银河标签3中.
Issue exists in Samsung galaxy Tab 3.
推荐答案
未知的原因.因为它可以在我测试过的设备(S6 5.0)上运行.您可能需要查看不推荐使用的getText()
方法的实现:
The reason of your problem is unknown. since it works on a device which I tested on (S6 5.0). You may want to look at the implementation of deprecated getText()
method:
public CharSequence getText() {
ClipData clip = getPrimaryClip();
if (clip != null && clip.getItemCount() > 0) {
return clip.getItemAt(0).coerceToText(mContext);
}
return null;
}
为了获取文本,它使用coerceToText()方法.根据此方法的说明:
In order to obtain the text It uses the method coerceToText() . according to description of this method:
* Turn this item into text, regardless of the type of data it
* actually contains.
因此,我认为不赞成使用getText()方法是出于性能方面的考虑或其他原因.
Therefore , I presume the deprecation of method getText() is due to a performance concern or something else.
无论如何.由于方法getText()
使用未弃用的API,因此,作为解决方法,如果调用推荐的API返回null,则可以使用此方法的某些部分的源(特别是方法coerceToText()
):
Anyway. Since method getText()
uses API which is not deprecated, as a workaround you can use some part of the source of this method(specifically, method coerceToText()
) if calling recommended API returns null:
ClipboardManager mclipboard =(ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
boolean isTextPlain = mclipboard.getPrimaryClip().getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
CharSequence text = null;
if (isTextPlain){
ClipData clipData = mclipboard.getPrimaryClip();
ClipData.Item item = clipData.getItemAt(0);
if ( item!= null ){
text = item.getText();
if (text == null){
// taken from source of clipData.getText() method
text = item.coerceToText(this);
}
}
}
这篇关于使用getPrimaryClip()复制数据给定{text/plain {NULL}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!