{"id":656,"date":"2016-12-23T08:25:38","date_gmt":"2016-12-22T23:25:38","guid":{"rendered":"http:\/\/blue-bear.jp\/kb\/?p=656"},"modified":"2016-12-23T08:25:38","modified_gmt":"2016-12-22T23:25:38","slug":"android-%e3%82%ab%e3%83%a1%e3%83%a9%e3%81%a8%e3%82%ae%e3%83%a3%e3%83%a9%e3%83%aa%e3%83%bc%e3%80%81%e3%81%9d%e3%82%8c%e3%81%9e%e3%82%8c%e3%81%8b%e3%82%89%e7%94%bb%e5%83%8f%e3%82%92%e5%8f%96%e5%be%97","status":"publish","type":"post","link":"https:\/\/blue-bear.jp\/kb\/android-%e3%82%ab%e3%83%a1%e3%83%a9%e3%81%a8%e3%82%ae%e3%83%a3%e3%83%a9%e3%83%aa%e3%83%bc%e3%80%81%e3%81%9d%e3%82%8c%e3%81%9e%e3%82%8c%e3%81%8b%e3%82%89%e7%94%bb%e5%83%8f%e3%82%92%e5%8f%96%e5%be%97\/","title":{"rendered":"[android] \u30ab\u30e1\u30e9\u3068\u30ae\u30e3\u30e9\u30ea\u30fc\u3001\u305d\u308c\u305e\u308c\u304b\u3089\u753b\u50cf\u3092\u53d6\u5f97"},"content":{"rendered":"<h2>\u30ab\u30e1\u30e9\u3068\u30ae\u30e3\u30e9\u30ea\u30fc\u3001\u305d\u308c\u305e\u308c\u304b\u3089\u753b\u50cf\u3092\u53d6\u5f97<\/h2>\n<p>\u30ab\u30e1\u30e9\u3068\u30ae\u30e3\u30e9\u30ea\u30fc\u306e\u9078\u629e\u80a2\u3092\u8868\u793a\u3057\u3001\u30e6\u30fc\u30b6\u304c\u9078\u629e\u3057\u305f\u753b\u50cf\u3092ImageView\u306b\u8868\u793a\u3055\u305b\u308b<\/p>\n<p>Manifest.xml<\/p>\n<pre>\r\n    <uses-permission android:name=\"android.permission.CAMERA\" \/>\r\n    <uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" \/>\r\n<\/pre>\n<p>Activity.java<br \/>\n\uff0aChooser\u306bGallary\u306eIntent\u3068Camera\u306eIntent\u3092\u767b\u9332<br \/>\n\uff0aonActivityResult\u3067\u7d50\u679c\u3092\u53d7\u3051\u53d6\u3063\u3066\u753b\u50cf\u8868\u793a\u3057\u3066\u3044\u308b<\/p>\n<pre>\r\nprivate ImageButton closeButton;\r\n    private ImageButton photoButton;\r\n\r\n    private Uri resultUri;\r\n    private static final int REQUEST_CHOOSER = 1000;\r\n    private final static int REQUEST_PERMISSION = 1002;\r\n    private String filePath;\r\n    private Uri cameraUri;\r\n    private File  cameraFile;\r\n    private Intent intentCamera;\r\n\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n\r\n        photoButton = (ImageButton)findViewById(R.id.photo_button);\r\n        photoButton.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                _showGallery(); \/\/_showGallery\u30e1\u30bd\u30c3\u30c9\u3092\u8d77\u52d5\r\n            }\r\n        });\r\n    }\r\n\r\n    private void _showGallery() {\r\n\r\n        \/\/\u30ab\u30e1\u30e9\u306e\u8d77\u52d5Intent\u306e\u7528\u610f\r\n        if (Build.VERSION.SDK_INT >= 23) {\r\n            checkPermission();\r\n        }\r\n        else {\r\n            intentCamera = cameraIntent(); \/\/cameraIntent\u3068\u3044\u3046Intent\u3092\u8fd4\u3059\r\n        }\r\n\r\n        \/\/ \u30ae\u30e3\u30e9\u30ea\u30fc\u7528\u306eIntent\u4f5c\u6210\r\n        Intent intentGallery;\r\n        if (Build.VERSION.SDK_INT < 19) {\r\n            intentGallery = new Intent(Intent.ACTION_GET_CONTENT);\r\n            intentGallery.setType(\"image\/*\");\r\n        } else {\r\n            intentGallery = new Intent(Intent.ACTION_OPEN_DOCUMENT);\r\n            intentGallery.addCategory(Intent.CATEGORY_OPENABLE);\r\n            intentGallery.setType(\"image\/jpeg\");\r\n        }\r\n\r\n        \/\/Chooser\u306bGallary\u306eIntent\u3068Camera\u306eIntent\u3092\u767b\u9332\r\n        Intent intent = Intent.createChooser(intentGallery, \"Select Image\");\r\n        if(intentCamera!=null){\r\n            intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {intentCamera});\r\n        }\r\n        startActivityForResult(intent, REQUEST_CHOOSER);\r\n    }\r\n\r\n    \/\/\r\n    @Override\r\n    protected void onActivityResult(int requestCode, int resultCode, Intent data) {\r\n        super.onActivityResult(requestCode, resultCode, data);\r\n\r\n        if(requestCode == REQUEST_CHOOSER) {\r\n\r\n            if(resultCode != RESULT_OK) {\r\n                \/\/ \u30ad\u30e3\u30f3\u30bb\u30eb\u6642\r\n                return ;\r\n            }\r\n            \r\n            \/\/data\u304cnull\u306e\u5834\u5408\u306f\u30ae\u30e3\u30e9\u30ea\u30fc\u3067\u306f\u306a\u304f\u30ab\u30e1\u30e9\u304b\u3089\u306e\u53d6\u5f97\u3068\u5224\u5b9a\u3057\u30ab\u30e1\u30e9\u306eUri\u3092\u4f7f\u3046\r\n            resultUri = (data != null ? data.getData() : cameraUri);\r\n\r\n            if(resultUri == null) {\r\n                \/\/ \u53d6\u5f97\u5931\u6557\r\n                Toast.makeText(this, \"Error.Try again.\", Toast.LENGTH_LONG).show();\r\n                return;\r\n            }\r\n\r\n            \/\/ \u30ae\u30e3\u30e9\u30ea\u30fc\u3078\u30b9\u30ad\u30e3\u30f3\u3092\u4fc3\u3059\r\n            MediaScannerConnection.scanFile(\r\n                    this,\r\n                    new String[]{resultUri.getPath()},\r\n                    new String[]{\"image\/jpeg\"},\r\n                    null\r\n            );\r\n\r\n            \/\/ \u753b\u50cf\u3092\u8a2d\u5b9a\r\n            ImageView imageView = (ImageView)findViewById(R.id.photo);\r\n            imageView.setImageURI(resultUri);\r\n\r\n        }\r\n    }\r\n\r\n    private Intent cameraIntent(){\r\n        \/\/ \u4fdd\u5b58\u5148\u306e\u30d5\u30a9\u30eb\u30c0\u30fc\u3092\u4f5c\u6210\r\n        File cameraFolder = new File(\r\n                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), \"IMG\"\r\n        );\r\n        cameraFolder.mkdirs();\r\n\r\n        \/\/ \u4fdd\u5b58\u30d5\u30a1\u30a4\u30eb\u540d\r\n        String fileName = new SimpleDateFormat(\"ddHHmmss\").format(new Date());\r\n        filePath = cameraFolder.getPath() +\"\/\" + fileName + \".jpg\";\r\n        Log.d(\"debug\",\"filePath:\"+filePath);\r\n\r\n        \/\/ capture\u753b\u50cf\u306e\u30d5\u30a1\u30a4\u30eb\u30d1\u30b9\r\n        cameraFile = new File(filePath);\r\n        cameraUri = Uri.fromFile(cameraFile);\r\n\r\n        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);\r\n        intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);\r\n\r\n        return intent;\r\n    }\r\n\r\n    \/\/ Runtime Permission check\r\n    private void checkPermission(){\r\n        \/\/ \u65e2\u306b\u8a31\u53ef\u3057\u3066\u3044\u308b\r\n        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){\r\n            cameraIntent();\r\n        }\r\n        \/\/ \u62d2\u5426\u3057\u3066\u3044\u305f\u5834\u5408\r\n        else{\r\n            requestLocationPermission();\r\n        }\r\n    }\r\n\r\n    \/\/ \u8a31\u53ef\u3092\u6c42\u3081\u308b\r\n    private void requestLocationPermission() {\r\n        if (ActivityCompat.shouldShowRequestPermissionRationale(this,\r\n                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {\r\n            ActivityCompat.requestPermissions(ClubCreationImageActivity.this,\r\n                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION);\r\n\r\n        } else {\r\n            Toast toast = Toast.makeText(this, \"Camera function is disabled\", Toast.LENGTH_SHORT);\r\n            toast.show();\r\n\r\n            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,}, REQUEST_PERMISSION);\r\n\r\n        }\r\n    }\r\n<\/pre>\n<p>\uff0a\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u4e0a\u3067\u516c\u958b\u3055\u308c\u3066\u3044\u308b\u30b3\u30fc\u30c9\u3092\u5229\u7528\u3057\u305f\u304c\u3001\u30ab\u30e1\u30e9Intent\u3092\u4f5c\u6210\u3059\u308b\u5ea6\u306b<br \/>\n\u8b0e\u306e\u753b\u50cf\u304c\u30ae\u30e3\u30e9\u30ea\u30fc\u306b\u767b\u9332\u3055\u308c\u3066\u5897\u6b96\u3059\u308b\u306e\u3067\u624b\u3092\u52a0\u3048\u305f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30ab\u30e1\u30e9\u3068\u30ae\u30e3\u30e9\u30ea\u30fc\u3001\u305d\u308c\u305e\u308c\u304b\u3089\u753b\u50cf\u3092\u53d6<\/p>\n","protected":false},"author":1,"featured_media":383,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[61,62,63],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/656"}],"collection":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/comments?post=656"}],"version-history":[{"count":1,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/656\/revisions"}],"predecessor-version":[{"id":657,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/656\/revisions\/657"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media\/383"}],"wp:attachment":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media?parent=656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/categories?post=656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/tags?post=656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}