{"id":1298,"date":"2021-04-04T16:10:28","date_gmt":"2021-04-04T08:10:28","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1298"},"modified":"2021-04-19T22:37:30","modified_gmt":"2021-04-19T14:37:30","slug":"android-%e5%9c%96%e7%89%87%e8%ae%80%e5%8f%96%e5%bd%b1%e7%89%87%e8%ae%80%e5%8f%96%e9%9f%b3%e6%aa%94%e8%ae%80%e5%8f%96%e5%85%b6%e4%bb%96%e6%aa%94%e6%a1%88%e8%ae%80%e5%8f%96saf","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-%e5%9c%96%e7%89%87%e8%ae%80%e5%8f%96%e5%bd%b1%e7%89%87%e8%ae%80%e5%8f%96%e9%9f%b3%e6%aa%94%e8%ae%80%e5%8f%96%e5%85%b6%e4%bb%96%e6%aa%94%e6%a1%88%e8%ae%80%e5%8f%96saf\/","title":{"rendered":"Android \u5716\u7247\u8b80\u53d6&#038;\u5f71\u7247\u8b80\u53d6&#038;\u97f3\u6a94\u8b80\u53d6&#038;\u5176\u4ed6\u6a94\u6848\u8b80\u53d6SAF"},"content":{"rendered":"<h1>Android \u5716\u7247\u8b80\u53d6&#038;\u5f71\u7247\u8b80\u53d6&#038;\u97f3\u6a94\u8b80\u53d6&#038;\u5176\u4ed6\u6a94\u6848\u8b80\u53d6SAF<\/h1>\n<ol>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u5716\u7247\u8b80\u53d6\u5f71\u7247\u8b80\u53d6\u97f3\u6a94\u8b80\u53d6\u5176\u4ed6\u6a94\u6848\u8b80\u53d6saf\/fly\/android\/#a\">\u8b80\u53d6\u524d\u8981\u5148\u53d6\u5f97\u6b0a\u9650<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u5716\u7247\u8b80\u53d6\u5f71\u7247\u8b80\u53d6\u97f3\u6a94\u8b80\u53d6\u5176\u4ed6\u6a94\u6848\u8b80\u53d6saf\/fly\/android\/#b\">\u5716\u7247\u8b80\u53d6(Images)<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u5716\u7247\u8b80\u53d6\u5f71\u7247\u8b80\u53d6\u97f3\u6a94\u8b80\u53d6\u5176\u4ed6\u6a94\u6848\u8b80\u53d6saf\/fly\/android\/#c\">\u5f71\u7247\u8b80\u53d6(Video)<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u5716\u7247\u8b80\u53d6\u5f71\u7247\u8b80\u53d6\u97f3\u6a94\u8b80\u53d6\u5176\u4ed6\u6a94\u6848\u8b80\u53d6saf\/fly\/android\/#d\">\u97f3\u6a94\u8b80\u53d6(Video)<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u5716\u7247\u8b80\u53d6\u5f71\u7247\u8b80\u53d6\u97f3\u6a94\u8b80\u53d6\u5176\u4ed6\u6a94\u6848\u8b80\u53d6saf\/fly\/android\/#e\">\u5176\u4ed6\u6a94\u6848\u8b80\u53d6(\u5229\u7528SAF)<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u5716\u7247\u8b80\u53d6\u5f71\u7247\u8b80\u53d6\u97f3\u6a94\u8b80\u53d6\u5176\u4ed6\u6a94\u6848\u8b80\u53d6saf\/fly\/android\/#f\">\u6a94\u6848\u5132\u5b58\u5230\u6a5f\u8eab\u5916\u90e8\u5132\u5b58<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h3>1.\u8b80\u53d6\u524d\u8981\u5148\u53d6\u5f97\u6b0a\u9650<\/h3>\n<h5>Manifest<\/h5>\n<pre><code class=\"language-XML line-numbers\">&lt;uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\"\/&gt;\n<\/code><\/pre>\n<h5>Activity<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">const val REQUEST_READ_EXTERNAL_STORAGE = 3\n\nrequestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_READ_EXTERNAL_STORAGE)\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h3>2.\u5716\u7247\u8b80\u53d6(Images)<\/h3>\n<h5>Uri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI)<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">val cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null)\n\ncursor?.apply {\n    while (moveToNext()) {\n        val id = getLong(getColumnIndexOrThrow(MediaStore.MediaColumns._ID))\n        val uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)\n        uriToFileApiQ(uri, false)\n    }\n    cursor.close()\n}\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h3>3.\u5f71\u7247\u8b80\u53d6(Video)<\/h3>\n<h5>Uri(MediaStore.Video.Media.EXTERNAL_CONTENT_URI)<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">val cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, null)\n\ncursor?.apply {\n    while (moveToNext()) {\n        val id = getLong(getColumnIndexOrThrow(MediaStore.MediaColumns._ID))\n        val uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id)\n        uriToFileApiQ(uri, false)\n    }\n    cursor.close()\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h3>4.\u97f3\u6a94\u8b80\u53d6(Video)<\/h3>\n<h5>Uri(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI)<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">val cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null)\n\ncursor?.apply {\n    while (moveToNext()) {\n        val id = getLong(getColumnIndexOrThrow(MediaStore.MediaColumns._ID))\n        val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id)\n        uriToFileApiQ(uri, false)\n    }\n    cursor.close()\n}\n<\/code><\/pre>\n<p><a id=\"e\"><\/a><\/p>\n<h3>5.\u5176\u4ed6\u6a94\u6848\u8b80\u53d6(\u5229\u7528SAF)<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">const val SAF_REQUEST = 0\n\nval intent = Intent(Intent.ACTION_OPEN_DOCUMENT)\nintent.type = \"application\/pdf\"\n\/\/\u958b\u555f\u591a\u9078\nintent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)\nstartActivityForResult(intent, SAF_REQUEST)\n<\/code><\/pre>\n<h5>\u56de\u50b3\u55ae\u7b46\u8207\u591a\u7b46<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {\n    super.onActivityResult(requestCode, resultCode, data)\n\n    if (requestCode == SAF_REQUEST && resultCode == Activity.RESULT_OK) {\n        \/\/\u55ae\u7b46\n        data?.data?.let {\n            val uri = Uri.parse(it.toString())\n            uriToFileApiQ(uri, true)\n        }\n\n        \/\/\u591a\u7b46\n        data?.clipData?.let {\n            for (i in 0 until it.itemCount) {\n                val uri = Uri.parse(it.getItemAt(i).uri.toString())\n                uriToFileApiQ(uri, true)\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"f\"><\/a><\/p>\n<h3>6.\u6a94\u6848\u5132\u5b58\u5230\u6a5f\u8eab\u5916\u90e8\u5132\u5b58<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">@SuppressLint(\"SimpleDateFormat\")\nprivate fun uriToFileApiQ(uri: Uri, pdf: Boolean): File? {\n    var file: File? = null\n    if (uri.scheme == ContentResolver.SCHEME_FILE) {\n        file = File(uri.toString())\n    } else if (uri.scheme == ContentResolver.SCHEME_CONTENT) {\n        var date: String\n        val info = contentResolver.query(uri, null, null, null, null)\n        info?.run {\n            moveToFirst()\n            \/\/\u6a94\u6848\u5927\u5c0f\n            val size = getString(getColumnIndexOrThrow(MediaStore.Images.Media.SIZE))\n            date = if (!pdf) {\n                \/\/\u6a94\u6848\u5efa\u7acb\u65e5\u671f(\u6642\u9593\u6233\u8f49\u63db)\n                val dateAdded =\n                    getString(getColumnIndexOrThrow(MediaStore.Images.Media.DATE_ADDED))\n                val sdf = SimpleDateFormat(\"yyyy\u5e74MM\u6708dd\u65e5 HH\u9edemm\u5206ss\u79d2\")\n                sdf.format(Date(dateAdded.toLong() * 1000))\n            } else {\n                \"\"\n            }\n\n            \/\/\u6a94\u6848\u540d\u7a31\n            val fileName =\n                getString(getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME))\n\n            if (size.isNullOrEmpty()) return@run\n\n            \/\/\u8f38\u5165\u6d41\n            val inputStream = contentResolver.openInputStream(uri)\n\n            \/\/\u8f38\u51fa\u6d41\n            val filePath = File(getExternalFilesDir(\"myPhoto\")?.absolutePath, \"<span class=\"katex math inline\">date<\/span>fileName\")\n            val fos = FileOutputStream(filePath)\n\n            FileUtils.copy(inputStream!!, fos)\n            file = filePath\n\n            fos.close()\n            inputStream.close()\n            info.close()\n        }\n    }\n    return file\n}\n<\/code><\/pre>\n<pre><code class=\"language-Kotlin line-numbers\">MediaStore.Images.Media.RELATIVE_PATH \/\/\u53d6\u5f97\u5716\u7247\u4e0a\u5c64\u8def\u5f91\nMediaStore.Images.Media.DISPLAY_NAME  \/\/\u5716\u7247\u540d\u7a31\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Android \u5716\u7247\u8b80\u53d6&#038;\u5f71\u7247\u8b80\u53d6&#038;\u97f3\u6a94\u8b80\u53d6&#038;\u5176\u4ed6\u6a94\u6848\u8b80\u53d6SAF \u8b80\u53d6\u524d\u8981\u5148\u53d6\u5f97 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,15,153],"class_list":["post-1298","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-kotlin","tag-storage"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1298","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1298"}],"version-history":[{"count":6,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1298\/revisions"}],"predecessor-version":[{"id":1328,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1298\/revisions\/1328"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}