{"id":935,"date":"2023-03-11T18:00:09","date_gmt":"2023-03-11T10:00:09","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=935"},"modified":"2023-04-29T15:44:13","modified_gmt":"2023-04-29T07:44:13","slug":"android-getcontentresolver-query-get-file-path-nullpointerexception","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-getcontentresolver-query-get-file-path-nullpointerexception\/","title":{"rendered":"Android getContentResolver().query \u83b7\u53d6\u6587\u4ef6\u8def\u5f84NullPointerException"},"content":{"rendered":"<p>\u9488\u5bf9Android 6.0\u7cfb\u7edf\u6d4b\u8bd5\u5982\u4e0b\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">@Override\nprotected void onActivityResult(int requestCode, int resultCode, Intent data) {\n    super.onActivityResult(requestCode, resultCode, data);\n\n    Uri fileUri = data.getData();\n    String[] filePathColumn = { MediaStore.Images.Media.DATA };\n\n    Cursor cursor = getContentResolver().query(fileUri, filePathColumn, null, null, null);\n    cursor.moveToFirst();\n    String filePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));\n    cursor.close();\n}<\/code><\/pre>\n<p>cursor\u7684\u7ed3\u679c\u4e3anull\uff0c\u67e5\u627e\u539f\u56e0\uff0c\u5f97\u5230\u5982\u4e0b\u7ed3\u8bba\uff1a<\/p>\n<p>\u5bf9\u4e8eAndroid 6.0\u53ca\u540e\u7eed\u7248\u672c\uff0cUri\u7684\u5f62\u5f0f\u53d1\u751f\u53d8\u5316\uff0c\u6b64\u65f6\u5982\u679c\u8981\u83b7\u53d6\u56fe\u50cf\u7684\u8def\u5f84\uff0c\u53ef\u4ee5\u6309\u7167\u5982\u4e0b\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-java\">@Override\nprotected void onActivityResult(int requestCode, int resultCode, Intent data) {\n    super.onActivityResult(requestCode, resultCode, data);\n\n    Uri fileUri = data.getData();\n    String[] filePathColumn = { MediaStore.Images.Media.DATA };\n\n    String filePath = null;\n    if(Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.M) {\n        Cursor cursor = getContentResolver().query(fileUri, filePathColumn, null, null, null);\n        cursor.moveToFirst();\n        filePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));\n        cursor.close();\n    } else {\n        filePath = fileUri.getPath();\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9488\u5bf9Android 6.0\u7cfb\u7edf\u6d4b\u8bd5\u5982\u4e0b\uff1a @Override protected void onActivit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-935","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=935"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/935\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}