{"id":2336,"date":"2013-11-22T15:19:37","date_gmt":"2013-11-22T06:19:37","guid":{"rendered":"https:\/\/www.united-bears.co.jp\/blog\/?p=2336"},"modified":"2013-11-22T15:19:37","modified_gmt":"2013-11-22T06:19:37","slug":"uriandroid-net-uri%e3%81%a8-filejava-io-file-%e3%81%ae%e5%a4%89%e6%8f%9b","status":"publish","type":"post","link":"https:\/\/www.united-bears.co.jp\/blog\/archives\/2336","title":{"rendered":"Uri(android.net.Uri)\u3068 File(java.io.File) \u306e\u5909\u63db"},"content":{"rendered":"<p>Android \u3067\u30d5\u30a1\u30a4\u30eb\u306e\u60c5\u5831\u3092\u6271\u3046\u5834\u5408\u3001Java \u306e\u6a19\u6e96\u7684\u306a\u30af\u30e9\u30b9 <a href=\"http:\/\/developer.android.com\/reference\/java\/io\/File.html\" target=\"_blanK\">java.io.File<\/a>\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u3068\u3001<a href=\"http:\/\/developer.android.com\/reference\/android\/net\/Uri.html\" target=\"_blank\">android.net.Uri<\/a>\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u304c\u3042\u308b\u3002File \u30af\u30e9\u30b9\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u306f\u3001Android \u4e0a\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3084\u30d5\u30a1\u30a4\u30eb\u3092\u76f4\u63a5\u6307\u5b9a\u3059\u308b(\u203b)\u3002\u4e00\u65b9 Uri \u306f java.net.URI \u3068\u540c\u69d8\u306b RFC 2396\u3067\u5b9a\u7fa9\u3055\u308c\u305f URI \u53c2\u7167\u3092\u8868\u3059\u3002java.net.URI \u3068\u7570\u306a\u308a\u3001Uri \u306f\u5165\u529b\u30c1\u30a7\u30c3\u30af\u3092\u307b\u3068\u3093\u3069\u884c\u308f\u306a\u3044\u3088\u3046\u3060\u3002Uri \u306e\u30e1\u30bd\u30c3\u30c9\u304c\u30b9\u30ed\u30fc\u3059\u308b Exception \u3092\u307f\u308b\u3068\u3001NullPointerException\u3001UnsupportedOperationException \u3057\u304b\u306a\u304b\u3063\u305f\u3002<\/p>\n<p>\u203bAndroid \u3067\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u540d\u306f UTF-8\u306e\u30d0\u30a4\u30c8\u30b7\u30fc\u30b1\u30f3\u30b9\u3067 OS \u3068\u3084\u308a\u3068\u308a\u3059\u308b\u3002<\/p>\n<p>\u4ed6\u306e Activity \u3068\u9023\u643a\u3057\u306a\u3044\u3001\u60c5\u5831\u3082\u53d6\u5f97\u3057\u306a\u3044\u3068\u3044\u3046\u306e\u3067\u3042\u308c\u3070 File \u3060\u3051\u3067\u4e8b\u8db3\u308a\u308b\u306e\u3060\u304c\u3001Intent \u7d4c\u7531\u3067\u30c7\u30fc\u30bf\u3092\u53d7\u3051\u53d6\u308b Intent.getData()\u30e1\u30bd\u30c3\u30c9\u306f Uri \u304c\u623b\u308a\u5024\u306b\u306a\u3063\u3066\u3044\u308b\u3002\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u3068\u304d\u306f File \u30af\u30e9\u30b9\u3092\u4f7f\u3063\u3066\u3044\u305f\u306e\u3067\u3001File \u3068 Uri \u306e\u5909\u63db\u304c\u5fc5\u8981\u306b\u306a\u3063\u305f\u3002File \u304b\u3089 Uri \u306e\u5909\u63db\u306f\u7c21\u5358\u3060\u304c\u3001Uri \u304b\u3089 File \u306b\u3059\u308b\u306e\u306f\u9762\u5012\u3060\u3063\u305f\u306e\u3067\u30e1\u30e2\u3068\u3057\u3066\u6b8b\u3059\u3053\u3068\u306b\u3057\u305f\u3002<\/p>\n<h2>File \u304b\u3089 Uri \u3078\u306e\u5909\u63db<\/h2>\n<p>File \u304b\u3089 Uri \u3078\u306e\u5909\u63db\u306f\u3001Uri \u306b static \u30e1\u30bd\u30c3\u30c9\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u7c21\u5358\u3060\u3002\u5909\u63db\u3057\u305f\u3044 File \u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092 fromFile(File)\u30e1\u30bd\u30c3\u30c9\u306b\u6e21\u3059\u3060\u3051\u3067\u3088\u3044\u3002<\/p>\n<pre>File file = new File(...);\r\nUri uri = Uri.fromFile(file);<\/pre>\n<h2>Uri \u304b\u3089 File \u3078\u306e\u5909\u63db<\/h2>\n<p>\u6700\u521d\u306b\u66f8\u3044\u3066\u3044\u305f\u30b3\u30fc\u30c9\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5358\u7d14\u306a\u3082\u306e\u3060\u3002\u4ed6\u306e Activity \u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d7\u9818\u3059\u308b\u3060\u3051\u306a\u306e\u3067\u3001\u5fc5\u305a\u30d5\u30a1\u30a4\u30eb\u3060\u3068\u601d\u3063\u3066\u3044\u305f\u3002<\/p>\n<pre>Intent intent; \/\/ \u3069\u3053\u304b\u304b\u3089\u53d7\u3051\u53d6\u3063\u305f Intent\r\nUri = intent.getData();\r\nString path = uri.getPath();\r\nFile file = new File(path);<\/pre>\n<p>\u3057\u304b\u3057 Uri \u306f RFC 2396\u5f62\u5f0f\u306e URI \u53c2\u7167\u3067\u3001\u5e38\u306b\u5b9f\u30d5\u30a1\u30a4\u30eb\u3092\u6307\u3057\u3066\u3044\u308b\u3068\u306f\u9650\u3089\u306a\u3044\u3002\u540c\u69d8\u306b RF2396\u5f62\u5f0f\u306e URI \u53c2\u7167\u3067\u3042\u308b<a href=\"http:\/\/docs.oracle.com\/javase\/jp\/7\/api\/java\/net\/URI.html\" target=\"_blank\">java.net.URI<\/a>\u306b\u30b5\u30f3\u30d7\u30eb\u304c\u8f09\u3063\u3066\u3044\u308b\u306e\u3067\u5f15\u7528\u3059\u308b\u3002<\/p>\n<blockquote><p>\u4e0d\u900f\u660e URI \u306f\u3001\u30b9\u30ad\u30fc\u30e0\u56fa\u6709\u90e8\u5206\u304c\u30b9\u30e9\u30c3\u30b7\u30e5 (\u300c\/\u300d) \u3067\u59cb\u307e\u3089\u306a\u3044\u7d76\u5bfe URI \u3067\u3059\u3002\u4e0d\u900f\u660e URI \u306f\u305d\u308c\u4ee5\u4e0a\u89e3\u6790\u3055\u308c\u307e\u305b\u3093\u3002\u4e0d\u900f\u660e URI \u306e\u4f8b\u3092\u6b21\u306b\u793a\u3057\u307e\u3059\u3002<br \/>\nmailto:java-net@java.sun.com<br \/>\nnews:comp.lang.java<br \/>\nurn:isbn:096139210x<\/p>\n<p>\u968e\u5c64 URI \u306f\u3001\u30b9\u30ad\u30fc\u30e0\u56fa\u6709\u90e8\u5206\u304c\u30b9\u30e9\u30c3\u30b7\u30e5\u3067\u59cb\u307e\u308b\u7d76\u5bfe URI\u3001\u307e\u305f\u306f\u76f8\u5bfe URI (\u30b9\u30ad\u30fc\u30e0\u3092\u6307\u5b9a\u3057\u306a\u3044 URI) \u3067\u3059\u3002\u968e\u5c64 URI \u306e\u4f8b\u3092\u6b21\u306b\u793a\u3057\u307e\u3059\u3002<br \/>\nhttp:\/\/java.sun.com\/j2se\/1.3\/<br \/>\ndocs\/guide\/collections\/designfaq.html#28<br \/>\n..\/..\/..\/demo\/jfc\/SwingSet2\/src\/SwingSet2.java<br \/>\nfile:\/\/\/~\/calendar\n<\/p><\/blockquote>\n<p>\u203b\u300c\u4e0d\u900f\u660e\u300d\u306f\u3001opaque \u306e\u8a33\u3002\u300c\u30b9\u30ad\u30fc\u30e0\u300d\u306f scheme \u306e\u8a33(\u30b9\u30ad\u30fc\u30de\u3068\u8868\u8a18\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3082\u3042\u308b)\u3002<\/p>\n<p>Android \u3067 android.net.Uri \u3092 java.io.File \u306b\u5909\u63db\u3059\u308b\u5834\u5408\u3001Uri \u306e\u30b9\u30ad\u30fc\u30e0\u3092\u898b\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002\u30b9\u30ad\u30fc\u30e0\u304c\u300cfile\u300d\u3067\u3042\u308c\u3070\u6700\u521d\u306e\u30b3\u30fc\u30c9\u3067\u554f\u984c\u306a\u3044\u304c\u3001\u300ccontent\u300d\u306e\u5834\u5408\u306f\u3001Content Provider \u304b\u3089\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<pre>Intent intent; \/\/ \u3069\u3053\u304b\u304b\u3089\u53d7\u3051\u53d6\u3063\u305f Intent\r\nUri = intent.getData();\r\nContext context = getContext();\r\nString scheme = uri.getScheme();\r\n\r\nString path = null;\r\nif (\"file\".equals(scheme)) {\r\n    path = uri.getPath();\r\n} else if(\"content\".equals(scheme)) {\r\n    ContentResolver contentResolver = context.getContentResolver();\r\n    Cursor cursor = contentResolver.query(uri, new String[] { MediaColumns.DATA }, null, null, null);\r\n    if (cursor != null) {\r\n        cursor.moveToFirst();\r\n        path = cursor.getString(0);\r\n        cursor.close();\r\n    }\r\n}\r\nFile file = null == path ? null : new File(path);<\/pre>\n<p>ContentResolver.quiry(Uri, String[], String, String[], String) \u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u306f\u3001<a href=\"http:\/\/developer.android.com\/reference\/android\/content\/ContentResolver.html#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)\">API \u306e\u8aac\u660e<\/a>\u306b\u3088\u308c\u3070\u3001\u300cUri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder\u300d\u3068\u306a\u3063\u3066\u3044\u308b\u3002uri \u306f content \u30b9\u30ad\u30fc\u30e0\u306e URI\u3001projection \u306f\u691c\u7d22\u3059\u308b\u5217\u3092\u6307\u5b9a\u3059\u308b\u3002\u4eca\u56de\u306f\u6307\u5b9a\u3057\u3066\u3044\u306a\u3044\u304c\u3001selection \u306f\u30d5\u30a3\u30eb\u30bf\u30fc\u306e\u305f\u3081\u306e\u8ffd\u52a0\u6761\u4ef6\u3001selectionArgs \u306f selection \u3067\u6307\u5b9a\u3057\u305f\u30d7\u30ec\u30fc\u30b9\u30db\u30eb\u30c0\u30fc\u306b\u5f53\u3066\u306f\u3081\u308b\u5024\u3001sortOrder \u306f\u30bd\u30fc\u30c8\u9806\u5e8f\u3060\u3002ContentResolver \u306b\u95a2\u3057\u3066\u306f\u3001\u5225\u306e\u6a5f\u4f1a\u306b\u30e1\u30e2\u3092\u66f8\u304d\u305f\u3044\u3068\u601d\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android \u3067\u30d5\u30a1\u30a4\u30eb\u306e\u60c5\u5831\u3092\u6271\u3046\u5834\u5408\u3001Java \u306e\u6a19\u6e96\u7684\u306a\u30af\u30e9\u30b9 java.io.File\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u3068\u3001android.net.Uri\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u304c\u3042\u308b\u3002File \u30af\u30e9\u30b9\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u306f\u3001Android \u4e0a\u306e [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3],"tags":[],"class_list":["post-2336","post","type-post","status-publish","format-standard","hentry","category-android","category-android-develop"],"_links":{"self":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts\/2336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/comments?post=2336"}],"version-history":[{"count":17,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts\/2336\/revisions"}],"predecessor-version":[{"id":2353,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts\/2336\/revisions\/2353"}],"wp:attachment":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/media?parent=2336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/categories?post=2336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/tags?post=2336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}