{"id":1289,"date":"2021-03-30T22:33:19","date_gmt":"2021-03-30T14:33:19","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1289"},"modified":"2021-04-04T16:36:58","modified_gmt":"2021-04-04T08:36:58","slug":"android-%e5%85%a7%e5%bb%ba%e7%9b%b8%e6%a9%9f%e5%9c%96%e7%89%87%e5%84%b2%e5%ad%98%e5%9c%96%e7%89%87%e8%ae%80%e5%8f%96","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-%e5%85%a7%e5%bb%ba%e7%9b%b8%e6%a9%9f%e5%9c%96%e7%89%87%e5%84%b2%e5%ad%98%e5%9c%96%e7%89%87%e8%ae%80%e5%8f%96\/","title":{"rendered":"Android  \u5167\u5efa\u76f8\u6a5f \u5716\u7247\u5132\u5b58&#038;\u5716\u7247\u8b80\u53d6"},"content":{"rendered":"<h1>Android  \u5167\u5efa\u76f8\u6a5f \u5716\u7247\u5132\u5b58&#038;\u5716\u7247\u8b80\u53d6<\/h1>\n<ol>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u5716\u7247\u5132\u5b58\/fly\/android\/#a\">\u6a5f\u8eab\u5167\u90e8&#038;\u6a5f\u8eab\u5916\u90e8<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u5716\u7247\u5132\u5b58\/fly\/android\/#b\">\u5916\u90e8\u5132\u5b58<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h3>1.\u6a5f\u8eab\u5167\u90e8&#038;\u6a5f\u8eab\u5916\u90e8<\/h3>\n<h4>a.\u5275\u5efaFileProvider (xml>file_provider.xml)<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;paths&gt;\n    &lt;external-path name=\"external-path\" path=\".\"\/&gt;\n    &lt;external-cache-path name=\"external-cache-path\" path=\".\"\/&gt;\n    &lt;files-path name=\"files-path\" path=\".\"\/&gt;\n    &lt;cache-path name=\"cache-path\" path=\".\"\/&gt;\n&lt;\/paths&gt;\n<\/code><\/pre>\n<h4>b.Manifests\u8072\u660eProvider<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;provider\n    android:name=\"androidx.core.content.FileProvider\"\n    android:authorities=\"${applicationId}.provider\"\n    android:exported=\"false\"\n    android:grantUriPermissions=\"true\"&gt;\n    &lt;meta-data\n        android:name=\"android.support.FILE_PROVIDER_PATHS\"\n        android:resource=\"@xml\/file_provider\" \/&gt;\n&lt;\/provider&gt;\n<\/code><\/pre>\n<h4>c.\u8abf\u7528\u5167\u5efa\u76f8\u6a5f\u4e26\u8a2d\u5b9aUri<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">val file = File(cacheDir, \"${System.currentTimeMillis()}photo.jpg\")\nval imgUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + \".provider\", file)\n\nval intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)\nintent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri)\nstartActivityForResult(intent, 0)\n<\/code><\/pre>\n<h4>d.\u986f\u793a\u5716\u7247(\u5169\u7a2e)<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">imageView.setImageURI(imgUri)\n<\/code><\/pre>\n<pre><code class=\"language-Kotlin line-numbers\">val bitmap = BitmapFactory.decodeFile(file.absolutePath)\nimageView.setImageBitmap(bitmap)\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h3>2.\u5916\u90e8\u5132\u5b58<\/h3>\n<h4>a.\u8abf\u7528\u5167\u5efa\u76f8\u6a5f\u4e26\u8a2d\u5b9aUri<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">val contentValues = ContentValues()\n\ncontentValues.put(\n    MediaStore.Images.ImageColumns.DISPLAY_NAME,\n    \"${System.currentTimeMillis()}photo.jpg\"\n)\ncontentValues.put(MediaStore.Images.ImageColumns.MIME_TYPE, \"image\/jpeg\")\nimgUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)!!\n\nval intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)\nintent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri)\nstartActivityForResult(intent, 0)\n<\/code><\/pre>\n<h4>b.\u986f\u793a\u5716\u7247(\u5169\u7a2e)<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">imageView.setImageURI(imgUri)\n<\/code><\/pre>\n<pre><code class=\"language-Kotlin line-numbers\">val file = uriToFileApiQ(imgUri)\nval bitmap = BitmapFactory.decodeFile(file?.absolutePath)\nimageView.setImageBitmap(bitmap)\n\n@RequiresApi(Build.VERSION_CODES.Q)\nprivate fun uriToFileApiQ(uri: Uri): 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        val cursor: Cursor? = contentResolver.query(uri, null, null, null, null)\n        cursor?.run {\n            if (moveToFirst()) {\n                val displayName: String =\n                    getString(getColumnIndex(OpenableColumns.DISPLAY_NAME))\n                try {\n                    val `is`: InputStream = contentResolver.openInputStream(uri)!!\n                    val cache = File(\n                        filesDir.absolutePath,\n                        ((Math.random() + 1) * 1000).roundToInt().toString() + displayName\n                    )\n                    val fos = FileOutputStream(cache)\n                    FileUtils.copy(`is`, fos)\n                    file = cache\n                    fos.close()\n                    `is`.close()\n                } catch (e: IOException) {\n                    e.printStackTrace()\n                }\n            }\n            cursor.close()\n        }\n    }\n    return file\n}\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 \u5167\u5efa\u76f8\u6a5f \u5716\u7247\u5132\u5b58&#038;\u5716\u7247\u8b80\u53d6 \u6a5f\u8eab\u5167\u90e8&#038;\u6a5f\u8eab\u5916\u90e8 \u5916\u90e8\u5132\u5b58 1.\u6a5f\u8eab\u5167\u90e8&#038; &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,178,15],"class_list":["post-1289","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-contentresolverfileprovider","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1289","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=1289"}],"version-history":[{"count":5,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1289\/revisions"}],"predecessor-version":[{"id":1310,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1289\/revisions\/1310"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}