{"id":1834,"date":"2022-11-25T18:00:27","date_gmt":"2022-11-25T10:00:27","guid":{"rendered":"https:\/\/app.applebyme.cn\/cloud\/appshangjia\/1834.html"},"modified":"2022-11-25T18:00:27","modified_gmt":"2022-11-25T10:00:27","slug":"%e5%b0%8f%e7%b1%b3%e6%89%8b%e6%9c%ba%e8%ae%be%e5%a4%87%e5%85%a8%e5%b1%80%e6%8b%96%e6%8b%bd%e5%8a%9f%e8%83%bd%e6%8a%80%e6%9c%af%e9%80%82%e9%85%8d%e8%af%b4%e6%98%8e","status":"publish","type":"post","link":"https:\/\/app.applebyme.cn\/cloud\/appshangjia\/1834.html","title":{"rendered":"\u5c0f\u7c73\u624b\u673a\u8bbe\u5907\u5168\u5c40\u62d6\u62fd\u529f\u80fd\u6280\u672f\u9002\u914d\u8bf4\u660e"},"content":{"rendered":"<p><span class=\"wpcom_tag_link\">\u5c0f\u7c73<\/span><span class=\"wpcom_tag_link\">\u624b\u673a<\/span>\u8bbe\u5907\u5168\u5c40<span class=\"wpcom_tag_link\">\u62d6\u62fd<\/span>\u529f\u80fd\u6280\u672f\u9002\u914d\u8bf4\u660e<\/p>\n<h2 id=\"_0\">1.\u7b80\u4ecb<\/h2>\n<p><span class=\"wpcom_tag_link\">\u5b89\u5353<\/span>\u62d6\u62fd\u5206\u4eab\u529f\u80fd\u63d0\u4f9b\u4e86\u4e00\u79cd<span class=\"wpcom_tag_link\">\u8de8\u7a97\u53e3<\/span>\u4f20\u9012\u6570\u636e\u7684\u529f\u80fd\uff0c\u6587\u672c\u3001\u56fe\u50cf\u6216\u4efb\u4f55\u53ef\u4ee5\u7528uri\u8868\u793a\u7684\u6570\u636e\u90fd\u53ef\u4ee5\u901a\u8fc7\u62d6\u62fd\u4ece\u4e00\u4e2a\u7a97\u53e3\u4f20\u9012\u5230\u53e6\u4e00\u4e2a\u7a97\u53e3\u3002<\/p>\n<p>\u53ef\u53c2\u8003\u8c37\u6b4c\u5b98\u65b9\u6587\u6863\uff1aDrag and drop | Android Developers<\/p>\n<p><span class=\"wpcom_keyword_link\">app<\/span>\u9002\u914d\u62d6\u62fd\u529f\u80fd\u4e3b\u8981\u5206\u4e3a<strong>\u62d6\u51fa\u9002\u914d<\/strong>\u548c<strong>\u62d6\u5165\u9002\u914d<\/strong>\uff0c\u672c\u6587\u5c06\u5206\u522b\u7b80\u4ecb\u5176\u9002\u914d\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"_1\">2.\u62d6\u51fa\u9002\u914d<\/h2>\n<p>app\u5bf9\u4efb\u610fview\u8c03\u7528<strong>startDragAndDrop<\/strong>\u65b9\u6cd5\u5373\u53ef\u5b9e\u73b0\u62d6\u51fa\u3002\u672c\u7ae0\u5206\u522b\u5bf9\u62d6\u51fa\u6587\u5b57\u3001\u62d6\u51fa\u56fe\u7247\u3001\u62d6\u51fa\u4efb\u610f\u6587\u4ef6\u8fdb\u884c\u6f14\u793a\u3002<\/p>\n<h3 id=\"_1_0\">2.1.\u62d6\u51fa\u6587\u5b57<\/h3>\n<p>\u4f7f\u7528\u4e00\u4e2aTextView\u6765\u62d6\u51fa\u6587\u5b57\uff1a<\/p>\n<pre class=\"wp-block-code\"><code><em>\/\/ \u62d6\u51fa\u6587\u5b57\u793a\u4f8b<\/em>\nfindViewById&lt;TextView&gt;(R.id.drag_text_view).setOnLongClickListener { view -&gt; <em>\/\/ \u8bbe\u7f6e\u957f\u6309\u56de\u8c03<\/em>\n    val textView = view as TextView\n    val clipData = ClipData.newPlainText(\"label\", textView.text) <em>\/\/ \u6784\u5efa\u5b58\u653e\u6587\u672c\u7684ClipData<\/em>\n    <em>\/\/ \u8c03\u7528view.startDragAndDrop\u65b9\u6cd5\u5f00\u59cb\u62d6\u62fd<\/em>\n    textView.startDragAndDrop(clipData, <em>\/\/ \u4f20\u5165clipData<\/em>\n        View.DragShadowBuilder(textView), <em>\/\/ \u4f7f\u7528textView\u7684draw\u65b9\u6cd5\u7ed8\u5236\u62d6\u62fd\u7684\u56fe\u50cf<\/em>\n        <strong>null<\/strong>, <em>\/\/ \u4f20\u5165\u4e00\u4e2a\u672c\u5730\u6570\u636e\u5bf9\u8c61<\/em>\n        View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ) <em>\/\/ \u52a0\u5165\u8fd9\u4e9bflag\u5141\u8bb8\u8de8\u7a97\u53e3\u62d6\u62fd<\/em>\n    <strong>true<\/strong>\n}<\/code><\/pre>\n<p>\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528ClipData.newPlainText\u6784\u5efa\u4e00\u4e2a\u4fdd\u5b58\u6587\u672c\u7684clipData\uff0c\u518d\u8c03\u7528view.startDragAndDrop\u65b9\u6cd5\uff0c\u5c06\u8fd9\u4e2aclipData\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u5373\u53ef\u5b9e\u73b0\u6587\u5b57\u7684\u62d6\u51fa\u3002<\/p>\n<p>\u5b89\u5353\u7684EditText\u672c\u8eab\u5c31\u5b9e\u73b0\u4e86\u6587\u5b57\u7684\u62d6\u51fa\u548c\u62d6\u5165\uff0c\u4e0d\u9700\u8981\u989d\u5916\u9002\u914d\u3002<\/p>\n<h3 id=\"_1_1\">2.2.\u62d6\u51fa\u56fe\u7247<\/h3>\n<p>\u4f7f\u7528\u4e00\u4e2aImageView\u6765\u62d6\u51fa\u56fe\u7247\uff1a<\/p>\n<pre class=\"wp-block-code\"><code><em>\/\/ \u62d6\u51fa\u56fe\u7247\u793a\u4f8b<\/em>\nfindViewById&lt;ImageView&gt;(R.id.drag_image_view).setOnLongClickListener { imageView -&gt; <em>\/\/ \u8bbe\u7f6e\u957f\u6309\u56de\u8c03<\/em>\n    val imageUri = getFileUri(R.mipmap.drag_image, \"drag_image.png\") <em>\/\/ \u901a\u8fc7fileProvider\u751f\u6210\u56fe\u50cf\u6587\u4ef6uri<\/em>\n    val clipData = ClipData(\"label\", arrayOf(\"image\/png\"), ClipData.Item(imageUri)) <em>\/\/ \u4f7f\u7528imageUri\u6784\u5efaClipData<\/em>\n    <em>\/\/ \u8c03\u7528view.startDragAndDrop\u65b9\u6cd5\u5f00\u59cb\u62d6\u62fd<\/em>\n    imageView.startDragAndDrop(clipData, <em>\/\/ \u4f20\u5165clipData<\/em>\n        View.DragShadowBuilder(imageView), <em>\/\/ \u4f7f\u7528imageView\u7684draw\u65b9\u6cd5\u7ed8\u5236\u62d6\u62fd\u7684\u56fe\u50cf<\/em>\n        <strong>null<\/strong>, <em>\/\/ \u4f20\u5165\u4e00\u4e2a\u672c\u5730\u6570\u636e\u5bf9\u8c61<\/em>\n        View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ) <em>\/\/ \u52a0\u5165\u8fd9\u4e9bflag\u5141\u8bb8\u8de8\u7a97\u53e3\u62d6\u62fd<\/em>\n    <strong>true<\/strong> <em>\/\/ \u8fd4\u56detrue\u8868\u793a\u957f\u6309\u4e8b\u4ef6\u88ab\u5904\u7406\u4e86<\/em>\n}<\/code><\/pre>\n<p>\u5176\u4e2dgetFileUri\u65b9\u6cd5\u5c06\u5b89\u5353\u8d44\u6e90\u4e2d\u7684\u4e00\u5f20\u56fe\u7247\u4f5c\u4e3a\u6587\u4ef6\u4fdd\u5b58\u5230\u672c\u5730\uff0c\u518d\u4f7f\u7528FileProvider\u5f97\u5230\u56fe\u7247\u6587\u4ef6\u7684uri\uff0c\u5982\u679c\u5bf9\u5177\u4f53\u5b9e\u73b0\u6709\u5174\u8da3\u53ef\u4ee5\u9605\u8bfb\u6e90\u7801\u3002\u6700\u7ec8\u6211\u4eec\u662f\u5c06uri\u653e\u5165ClipData\u4e2d\uff0c\u518d\u8c03\u7528view.startDragAndDrop\u65b9\u6cd5\uff0c\u5c06\u8fd9\u4e2aclipData\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u5373\u53ef\u5b9e\u73b0\u56fe\u7247\u7684\u62d6\u51fa\u3002<\/p>\n<h3 id=\"_1_2\">2.3.\u62d6\u51fa\u4efb\u610f\u6587\u4ef6<\/h3>\n<p>\u4efb\u610f\u6587\u4ef6\u548c\u56fe\u7247\u4e00\u6837\uff0c\u4f7f\u7528\u4e00\u4e2auri\u6765\u8868\u793a\uff0c\u56e0<a href=\"https:\/\/app.applebyme.cn\/shengcheng-36.html\">\u7f51\u7ad9\u4e00\u952e\u751f\u6210APP<\/a>\u6b64\u62d6\u51fa\u4efb\u610f\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u548c\u62d6\u51fa\u56fe\u7247\u7c7b\u4f3c\u7684\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<p>\u4f7f\u7528\u4e00\u4e2aButton\u6765\u9009\u62e9\u4efb\u610f\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528\u4e00\u4e2aTextView\u6765\u663e\u793a\u6587\u4ef6uri\u5e76\u5b9e\u73b0\u6587\u4ef6\u7684\u62d6\u51fa\uff1a<\/p>\n<pre class=\"wp-block-code\"><code><em>\/\/ \u62d6\u51fa\u4efb\u610f\u6587\u4ef6\u793a\u4f8b<\/em>\nfindViewById&lt;Button&gt;(R.id.choose_file_button).setOnClickListener { chooseFile() } <em>\/\/ \u9009\u62e9\u6587\u4ef6\u6309\u94ae<\/em>\nmDragFileView = findViewById(R.id.drag_file_view)\nmDragFileView.setOnLongClickListener { <em>\/\/ \u8bbe\u7f6e\u957f\u6309\u56de\u8c03<\/em>\n    <strong>if<\/strong> (mFileUri != <strong>null<\/strong>) { <em>\/\/ \u9009\u62e9\u7684\u6587\u4ef6\u4fdd\u5b58\u5728mFileUri\uff0c\u5982\u679c\u5176\u4e0d\u4e3anull\u8868\u793a\u5df2\u7ecf\u9009\u62e9\u4e86\u4e00\u4e2a\u6587\u4ef6<\/em>\n        val clipData = ClipData.newRawUri(\"label\", mFileUri) <em>\/\/ \u4f7f\u7528mFileUri\u6784\u5efaClipData<\/em>\n        <em>\/\/ \u8c03\u7528view.startDragAndDrop\u65b9\u6cd5\u5f00\u59cb\u62d6\u62fd<\/em>\n        mDragFileView.startDragAndDrop(\n            clipData, <em>\/\/ \u4f20\u5165clipData<\/em>\n            View.DragShadowBuilder(mDragFileView), <em>\/\/ \u4f7f\u7528imageView\u7684draw\u65b9\u6cd5\u7ed8\u5236\u62d6\u62fd\u7684\u56fe\u50cf<\/em>\n            <strong>null<\/strong>, <em>\/\/ \u4f20\u5165\u4e00\u4e2a\u672c\u5730\u6570\u636e\u5bf9\u8c61<\/em>\n            View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ) <em>\/\/ \u52a0\u5165\u8fd9\u4e9bflag\u5141\u8bb8\u8de8\u7a97\u53e3\u62d6\u62fd<\/em>\n    }\n    <strong>true<\/strong> <em>\/\/ \u8fd4\u56detrue\u8868\u793a\u957f\u6309\u4e8b\u4ef6\u88ab\u5904\u7406\u4e86<\/em>\n}<\/code><\/pre>\n<h2 id=\"_2\">3.\u62d6\u5165\u9002\u914d<\/h2>\n<p>app\u5bf9\u4efb\u610fview\u6ce8\u518c<strong>OnDragListener<\/strong>\u76d1\u542c\u5668\u5373\u53ef\u5b9e\u73b0\u62d6\u5165\u5904\u7406\u3002\u672c\u7ae0\u5206\u522b\u5bf9\u62d6\u5165\u6587\u5b57\u3001\u62d6\u5165\u56fe\u7247\u8fdb\u884c\u6f14\u793a\u3002<\/p>\n<h3 id=\"_2_0\">3.1.\u62d6\u5165\u6587\u5b57<\/h3>\n<p>\u4f7f\u7528\u4e00\u4e2aTextView\u6765\u62d6\u5165\u6587\u5b57\uff1a<\/p>\n<pre class=\"wp-block-code\"><code><em>\/\/ \u62d6\u5165\u6587\u5b57\u793a\u4f8b<\/em>\nfindViewById&lt;TextView&gt;(R.id.drop_text_view).setOnDragListener { view, event -&gt; <em>\/\/ \u8bbe\u7f6e\u62d6\u62fd\u76d1\u542c\u5668<\/em>\n    val textView = view as TextView\n    <strong>when<\/strong> (event.action) { <em>\/\/ \u5bf9\u62d6\u62fd\u4e0d\u540c\u7684\u4e8b\u4ef6\u8fdb\u884c\u5904\u7406<\/em>\n        DragEvent.ACTION_DRAG_STARTED -&gt; {\n            val hasText = event.clipDescription.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) <em>\/\/ \u67e5\u627e\u662f\u5426\u6709\u6587\u5b57\u7c7b\u578b\u7684\u6570\u636e<\/em>\n            <strong>if<\/strong> (!hasText) { <em>\/\/ \u6ca1\u6709\u6587\u5b57\u7c7b\u578b\u7684\u6570\u636e<\/em>\n                <strong>return<\/strong>@setOnDragListener <strong>false<\/strong> <em>\/\/ \u8fd4\u56defalse\u4ee3\u8868\u8fd9\u6b21\u62d6\u62fd\u4e0d\u518d\u7ee7\u7eed\u63a5\u6536\u62d6\u62fd\u4e8b\u4ef6<\/em>\n            }\n        }\n        DragEvent.ACTION_DROP -&gt; { <em>\/\/ ACTION_DROP\u4e8b\u4ef6\u8868\u793a\u62d6\u62fd\u62ac\u624b\u7ed3\u675f\u7684\u65f6\u5019<\/em>\n            textView.text = event.clipData.getItemAt(0).text <em>\/\/ \u5c06\u6587\u5b57\u8bbe\u5230textView\u4e0a\u663e\u793a<\/em>\n        }\n    }\n    <strong>true<\/strong> <em>\/\/ \u8fd4\u56detrue\u4ee3\u8868\u62d6\u62fd\u4e8b\u4ef6\u88ab\u5904\u7406\u4e86<\/em>\n}<\/code><\/pre>\n<p>\u6211\u4eec\u53ea\u9700\u8981\u901a\u8fc7view.setOnDragListener\u65b9\u6cd5\u6ce8\u518c\u4e00\u4e2a\u76d1\u542c\u5668\uff0c\u5e76\u5728\u76d1\u542c\u5668\u91cc\u9762\u5904\u7406\u62d6\u62fd\u4e8b\u4ef6\u5373\u53ef\u5b9e\u73b0\u62d6\u5165\u3002\u5176\u4e2d\u5728ACTION_DRAG_STARTED\u4e8b\u4ef6\u4e2d\u5bf9\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u5224\u65ad\uff0c\u5982\u679c\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u6570\u636e\u7c7b\u578b\u5c31\u8fd4\u56defalse\u5373\u53ef\u4e0d\u518d\u63a5\u6536\u672c\u6b21\u62d6\u62fd\u4e8b\u4ef6\uff1b\u6700\u540e\u5728ACTION_DROP\u4e8b\u4ef6\u4e2d\u83b7\u53d6ClipData\u6570\u636e\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<h3 id=\"_2_1\">3.2.\u62d6\u5165\u56fe\u7247<\/h3>\n<p>\u4f7f\u7528\u4e00\u4e2aImageView\u6765\u62d6\u5165\u6587\u5b57\uff1a<\/p>\n<pre class=\"wp-block-code\"><code><em>\/\/ \u62d6\u5165\u56fe\u7247\u793a\u4f8b<\/em>\nfindViewById&lt;ImageView&gt;(R.id.drop_image_view).setOnDragListener { view, event -&gt; <em>\/\/ \u8bbe\u7f6e\u62d6\u62fd\u76d1\u542c\u5668<\/em>\n    val imageView = view as ImageView\n    <strong>when<\/strong> (event.action) { <em>\/\/ \u5bf9\u62d6\u62fd\u4e0d\u540c\u7684\u4e8b\u4ef6\u8fdb\u884c\u5904\u7406<\/em>\n        DragEvent.ACTION_DRAG_STARTED -&gt; {\n            val mimeTypes = event.clipDescription.filterMimeTypes(\"image\/*\") <em>\/\/ \u67e5\u627e\u662f\u5426\u6709\u56fe\u50cf\u7c7b\u578b\u7684\u6570\u636e<\/em>\n            <strong>if<\/strong> (mimeTypes == <strong>null<\/strong>) { <em>\/\/ \u6ca1\u6709\u56fe\u50cf\u7c7b\u578b\u7684\u6570\u636e<\/em>\n                <strong>return<\/strong>@setOnDragListener <strong>false<\/strong> <em>\/\/ \u8fd4\u56defalse\u4ee3\u8868\u8fd9\u6b21\u62d6\u62fd\u4e0d\u518d\u7ee7\u7eed\u63a5\u6536\u62d6\u62fd\u4e8b\u4ef6<\/em>\n            }\n        }\n        DragEvent.ACTION_DROP -&gt; { <em>\/\/ ACTION_DROP\u4e8b\u4ef6\u8868\u793a\u62d6\u62fd\u62ac\u624b\u7ed3\u675f\u7684\u65f6\u5019<\/em>\n            requestDragAndDropPermissions(event) <em>\/\/ \u7533\u8bf7\u8bfb\u53d6uri\u7684\u6743\u9650<\/em>\n            imageView.setImageURI(event.clipData.getItemAt(0).uri) <em>\/\/ \u5c06\u56fe\u50cfuri\u8bbe\u5230imageView\u4e0a\u663e\u793a<\/em>\n        }\n    }\n    <strong>true<\/strong> <em>\/\/ \u8fd4\u56detrue\u4ee3\u8868\u62d6\u62fd\u4e8b\u4ef6\u88ab\u5904\u7406\u4e86<\/em>\n}<\/code><\/pre>\n<p>\u4e0e\u62d6\u5165\u6587\u5b57\u4e0d\u540c\u7684\u5730\u65b9\u5728\u4e8e\uff0c\u56fe\u7247\u662f\u4e00\u4e2a\u7528uri\u8868\u793a\u7684\u6587\u4ef6\uff0c\u8981\u8bbf\u95ee\u8fd9\u4e2auri\u4e4b\u524d\u5fc5\u987b\u8c03\u7528Activity.requestDragAndDropPermissions\u65b9\u6cd5\u7533\u8bf7\u6743\u9650\u3002<\/p>\n<h3 id=\"_2_2\">3.3.\u62d6\u5165\u4efb\u610f\u6587\u4ef6<\/h3>\n<p>\u7c7b\u4f3c\u4e8e\u62d6\u5165\u56fe\u7247\uff0c\u60f3\u8981\u62d6\u5165\u4efb\u610f\u6587\u4ef6\u53ea\u9700\u8981\u5bf9\u62d6\u8fc7\u6765\u7684\u4efb\u610furi\u8fdb\u884c\u5904\u7406\u5373\u53ef\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"wp-block-code\"><code><em>\/\/ \u62d6\u5165\u4efb\u610f\u6587\u4ef6\u793a\u4f8b<\/em>\nview.setOnDragListener { view, event -&gt; <em>\/\/ \u8bbe\u7f6e\u62d6\u62fd\u76d1\u542c\u5668<\/em>\n    when (event.action) { <em>\/\/ \u5bf9\u62d6\u62fd\u4e0d\u540c\u7684\u4e8b\u4ef6\u8fdb\u884c\u5904\u7406<\/em>\n        DragEvent.ACTION_DROP -&gt; { <em>\/\/ ACTION_DROP\u4e8b\u4ef6\u8868\u793a\u62d6\u62fd\u62ac\u624b\u7ed3\u675f\u7684\u65f6\u5019<\/em>\n            requestDragAndDropPermissions(event) <em>\/\/ \u7533\u8bf7\u8bfb\u53d6uri\u7684\u6743\u9650<\/em>\n            <em>\/\/ \u5904\u7406uri<\/em>\n        }\n    }\n    <strong>true<\/strong> <em>\/\/ \u8fd4\u56detrue\u4ee3\u8868\u62d6\u62fd\u4e8b\u4ef6\u88ab\u5904\u7406\u4e86<\/em>\n}<\/code><\/pre>\n<h3 id=\"_2_3\">3.4.\u5224\u65ad\u62d6\u5165\u7684\u6570\u636e\u7c7b\u578b<\/h3>\n<p>\u62d6\u5165\u65b9\u901a\u5e38\u67092\u79cd\u65b9\u5f0f\u5224\u65ad\u62d6\u5165\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p><strong>\u65b9\u6cd51\uff1a\u53ef\u4ee5\u6839\u636eevent.clipDescription\u4e2d\u7684MIMETYPE\u6765\u5224\u65ad\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<p>MIMETYPE\u672c\u8eab\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8c37\u6b4c\u5bf9\u5176\u5728ClipDescription.java\u4e2d\u6709\u4e00\u4e9b\u9884\u5b9a\u4e49\uff1a<\/p>\n<pre class=\"wp-block-code\"><code><strong>public<\/strong> <strong>class<\/strong> <strong>ClipDescription<\/strong> <strong>implements<\/strong> <strong>Parcelable<\/strong> {\n    <em>\/**\n     * The MIME type for a clip holding plain text.\n     *\/<\/em>\n    <strong>public<\/strong> <strong>static<\/strong> <strong>final<\/strong> String MIMETYPE_TEXT_PLAIN = \"text\/plain\";\n\n    <em>\/**\n     * The MIME type for a clip holding HTML text.\n     *\/<\/em>\n    <strong>public<\/strong> <strong>static<\/strong> <strong>final<\/strong> String MIMETYPE_TEXT_HTML = \"text\/html\";\n\n    <em>\/**\n     * The MIME type for a clip holding one or more URIs.  This should be\n     * used for URIs that are meaningful to a user (such as an http: URI).\n     * It should &lt;em&gt;not&lt;\/em&gt; be used for a content: URI that references some\n     * other piece of data; in that case the MIME type should be the type\n     * of the referenced data.\n     *\/<\/em>\n    <strong>public<\/strong> <strong>static<\/strong> <strong>final<\/strong> String MIMETYPE_TEXT_URILIST = \"text\/uri-list\";\n\n    <em>\/**\n     * The MIME type for a clip holding an Intent.\n     *\/<\/em>\n    <strong>public<\/strong> <strong>static<\/strong> <strong>final<\/strong> String MIMETYPE_TEXT_INTENT = \"text\/vnd.android.intent\";\n\n    <em>\/**\n     * The MIME type for an activity. The ClipData must include intents with required extras\n     * {@link #EXTRA_PENDING_INTENT} and {@link Intent#EXTRA_USER}, and an optional\n     * {@link #EXTRA_ACTIVITY_OPTIONS}.\n     * @hide\n     *\/<\/em>\n    <strong>public<\/strong> <strong>static<\/strong> <strong>final<\/strong> String MIMETYPE_APPLICATION_ACTIVITY = \"application\/vnd.android.activity\";\n\n    <em>\/**\n     * The MIME type for a shortcut. The ClipData must include intents with required extras\n     * {@link Intent#EXTRA_SHORTCUT_ID}, {@link Intent#EXTRA_PACKAGE_NAME} and\n     * {@link Intent#EXTRA_USER}, and an optional {@link #EXTRA_ACTIVITY_OPTIONS}.\n     * @hide\n     *\/<\/em>\n    <strong>public<\/strong> <strong>static<\/strong> <strong>final<\/strong> String MIMETYPE_APPLICATION_SHORTCUT = \"application\/vnd.android.shortcut\";\n\n    <em>\/**\n     * The MIME type for a task. The ClipData must include an intent with a required extra\n     * {@link Intent#EXTRA_TASK_ID} of the task to launch.\n   <a href=\"https:\/\/app.applebyme.cn\/shengcheng-96.html\">app\u9690\u79c1\u653f\u7b56\u751f\u6210\u5668<\/a>  * @hide\n     *\/<\/em>\n    <strong>public<\/strong> <strong>static<\/strong> <strong>final<\/strong> String MIMETYPE_APPLICATION_TASK = \"application\/vnd.android.task\";\n\n    <em>\/**\n     * The MIME type for data whose type is otherwise unknown.\n     * &lt;p&gt;\n     * Per RFC 2046, the \"application\" media type is to be used for discrete\n     * data which do not fit in any of the other categories, and the\n     * \"octet-stream\" subtype is used to indicate that a body contains arbitrary\n     * binary data.\n     *\/<\/em>\n    <strong>public<\/strong> <strong>static<\/strong> <strong>final<\/strong> String MIMETYPE_UNKNOWN = \"application\/octet-stream\";\n    ......\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u9762\u53ea\u5b9a\u4e49\u4e86\u90e8\u5206\u6570\u636e\uff0c\u8fd8\u6709\u5176\u5b83\u6570\u636e\u9700\u8981app\u81ea\u5df1\u5b9a\u4e49\uff0c\u76ee\u524d\u6ca1\u6709\u4e00\u4e2a\u51c6\u786e\u7684\u89c4\u8303\uff0c\u901a\u5e38\u6765\u8bf4\u5982\u679c\u662f\u683c\u5f0f\u4e3ajpg\u7684\u56fe\u50cf\u6570\u636e\u5219MIMETYPE\u4e3a&#8221;image\/jpg&#8221;\u3002\u6240\u4ee5MIMETYPE\u53ef\u4ee5\u7528\u6765\u521d\u6b65\u5224\u5b9a\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u4e0d\u5b8c\u5168\u51c6\u786e\uff0c\u6bd5\u7adfMIMETYPE\u662f\u7531\u62d6\u51fa\u65b9\u8bbe\u5b9a\u7684\u503c\u3002<\/p>\n<p><strong>\u65b9\u6cd52\uff1a\u53ef\u4ee5\u5bf9\u4f20\u5165\u7684<\/strong><strong>uri<\/strong><strong>\u6765\u5224\u65ad\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ContentResolver\u7684getType\u65b9\u6cd5\u83b7\u53d6uri\u7684MIMETYPE\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7MimeTypeMap\u7684getExtensionFromMimeType\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u540e\u7f00\u540d\u3002<\/p>\n<pre class=\"wp-block-code\"><code>val mimeType = contentResolver.getType(uri) <em>\/\/ \u83b7\u53d6MIMETYPE<\/em>\nval extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType) <em>\/\/ \u83b7\u53d6\u6587\u4ef6\u540e\u7f00\u540d<\/em><\/code><\/pre>\n<p>\u901a\u8fc7\u6587\u4ef6\u540e\u7f00\u540d\u6211\u4eec\u5c31\u53ef\u4ee5\u51c6\u786e\u5224\u65ad\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p>\u7f16\u8f91\uff1ayimen\uff0c\u5982\u82e5\u8f6c\u8f7d\uff0c\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttps:\/\/www.yimenapp.com\/kb-yimen\/12491\/<\/p>\n<p>\u90e8\u5206\u5185\u5bb9\u6765\u81ea\u7f51\u7edc\u6295\u7a3f\uff0c\u5982\u6709\u4fb5\u6743\u8054\u7cfb\u7acb\u5220<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c0f\u7c73\u624b\u673a\u8bbe\u5907\u5168\u5c40\u62d6\u62fd\u529f\u80fd\u6280\u672f\u9002\u914d\u8bf4\u660e1.\u7b80\u4ecb\u5b89\u5353\u62d6\u62fd\u5206\u4eab\u529f\u80fd\u63d0\u4f9b\u4e86\u4e00\u79cd\u8de8\u7a97\u53e3\u4f20\u9012\u6570\u636e\u7684\u529f\u80fd\uff0c\u6587\u672c\u3001\u56fe\u50cf\u6216\u4efb\u4f55\u53ef\u4ee5\u7528uri\u8868\u793a\u7684\u6570\u636e\u90fd\u53ef\u4ee5\u901a\u8fc7\u62d6\u62fd\u4ece\u4e00\u4e2a\u7a97\u53e3\u4f20\u9012\u5230\u53e6\u4e00\u4e2a\u7a97\u53e3\u3002\u53ef\u53c2\u8003\u8c37\u6b4c\u5b98\u65b9\u6587\u6863\uff1aDrag and drop | Android Developersapp\u9002\u914d\u62d6\u62fd\u529f\u80fd\u4e3b\u8981\u5206\u4e3a\u62d6\u51fa\u9002\u914d\u548c\u62d6\u5165<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[1151,548,1288],"class_list":["post-1834","post","type-post","status-publish","format-standard","hentry","category-appshangjia","tag-1151","tag-548","tag-1288"],"_links":{"self":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts\/1834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/comments?post=1834"}],"version-history":[{"count":0,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts\/1834\/revisions"}],"wp:attachment":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/media?parent=1834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/categories?post=1834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/tags?post=1834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}