如果在WRITE_EXTERNAL_STORAGE权限中设置maxSdkVersion,为什么我的地图应用程序崩溃? [英] Why my map app crashes if I set maxSdkVersion in the WRITE_EXTERNAL_STORAGE permission?

查看:93
本文介绍了如果在WRITE_EXTERNAL_STORAGE权限中设置maxSdkVersion,为什么我的地图应用程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照此链接中的说明设置地图.并根据官方推荐的建议设置WRITE_EXTERNAL_STORAGE权限. Android文档,即仅针对18级及更低级别的API要求此权限.所以,我有这个清单:

I've set my map following the instructions in this link. And set the WRITE_EXTERNAL_STORAGE permission in accordance with the recomended by the Official Android Documentation, that is request this permission only for API level 18 and lower. So, I have this manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp" >

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                     android:maxSdkVersion="18"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps"/>
        <activity
            android:name=".activities.HomeActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/map_api_key"/>
    </application>

</manifest>

但是我遇到了这个错误:

But I am facing this error:

Caused by: java.lang.SecurityException: The Maps API requires the additional following
permissions to be set in the AndroidManifest.xml to ensure a correct behavior: 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

但是,正如清单中所示,我具有此许可权.一个细节是,如果我仅放置WRITE_EXTERNAL_STORAGE权限,而未指定maxSdkVersion值,则该应用程序将正常运行.

But, as it is showed in my manifest, I have this permission. One detail is if I put only the WRITE_EXTERNAL_STORAGE permission, without specifying the maxSdkVersion value, the app works properly.

这是Android的错误还是我在应用程序配置中忘记了某些内容?

Is this an Android bug or am I forgetting something in my app configuration?

更新1

这是logcat:

 FATAL EXCEPTION: main
 Process: com.example.map, PID: 15534
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.map/com.example.map.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
    at android.app.Activity.setContentView(Activity.java:1929)
    at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
    at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
    at com.example.map.MainActivity.onCreate(MainActivity.java:16)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    ... 11 more
 Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    at maps.e.ci.a(Unknown Source)
    at maps.e.ay.a(Unknown Source)
    at maps.e.ay.a(Unknown Source)
    at maps.e.al.a(Unknown Source)
    at maps.e.bh.a(Unknown Source)
    at maps.e.bg.a(Unknown Source)
    at etu.onTransact(SourceFile:107)
    at android.os.Binder.transact(Binder.java:361)
    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
    at com.google.android.gms.dynamic.a$4.b(Unknown Source)
    at com.google.android.gms.dynamic.a.a(Unknown Source)
    at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
    at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1093)
    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1195)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)

更新2

我的布局:

<fragment
    android:id="@+id/map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    class="com.google.android.gms.maps.SupportMapFragment" />

更新3

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

推荐答案

如果您检查了提供给

If you check the link you've provided to the <uses-permission> documentation, you'll see that it states that:

当您的应用要写入外部存储(getExternalFilesDir()提供的目录)中的自己的应用特定目录时,您的应用不再需要请求WRITE_EXTERNAL_STORAGE权限.

it's no longer necessary for your app to request the WRITE_EXTERNAL_STORAGE permission when your app wants to write to its own application-specific directories on external storage (the directories provided by getExternalFilesDir()).

似乎Maps API需要对其他目录的写权限.而且,如果您查看了您提供的其他链接的指定权限部分,它仍然列出所需的WRITE_EXTERNAL_STORAGE权限,但未提及API级别19的例外.

It would appear that the Maps API needs write access to other directories. And if you check the Specify permissions section of the other link you gave, it still lists the WRITE_EXTERNAL_STORAGE permission as required, with no mention of exception for API Level 19.

在检查了有关Maps API,Android API级别19和WRITE_EXTERNAL_STORAGE权限的限制的许多在线资源之后,我发现没有列出或引用使用Maps API的工作示例的源或示例.且该权限仅限于API级别18及以下.再次,我引用您提供的链接,该链接说在API级别19当您的应用程序要写入其在外部存储上的特定于应用程序的目录时,不需要此权限". (我要指出的是,在这种情况下,这是没有必要的,绝对不要建议"您完全设置此限制.)Maps API 需要写入外部存储目录的权限不是您的应用程序所拥有的. 无需指定maxSdkVersion值,应用程序即可正常运行"这一事实表明,即使在KitKat中,地图也需要该权限.错误正好告诉您.

After having checked many, many online resources with regard to the Maps API, Android API Level 19, and the restriction of the WRITE_EXTERNAL_STORAGE permission, I found no source or example that listed or referenced a working example using the Maps API with that permission restricted to API Level 18 and below. Again, I quote the link you've provided that says the permission is not needed in API Level 19 "when your app wants to write to its own application-specific directories on external storage". (I would point out that it merely says it is not necessary in that case, and in no way "recommends" that you set this restriction absolutely.) The Maps API needs write-permission to external storage directories that are not owned by your app. The fact that "without specifying the maxSdkVersion value, the app works properly" indicates that even in KitKat, Maps needs that permission. The error is telling you exactly that.

这篇关于如果在WRITE_EXTERNAL_STORAGE权限中设置maxSdkVersion,为什么我的地图应用程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆