繁体   English   中英

Cordova:Android上的sdcard访问不起作用

[英]Cordova: sdcard access on Android does not work

我目前正在尝试一项cordova测试项目,该项目包含下载文件以便随后在音乐播放器上播放。 基本上,应用程序应从我的服务器下载.mp3文件,并将其保存在应用程序存储或用户定义的路径(文件夹选择)中。 到目前为止,我已经成功下载了文件,我能够选择自定义路径并在File Explorer应用程序中播放音乐文件,但是不知何故它们未显示在Music Player应用程序或其他地方。 另外,即使我在配置文件中定义了外部访问权限,也无法将文件写入sdcard。 我当前用于访问文件系统的代码如下所示:

 app.log('Filesystem', 'Requesting filesystem...'); if (device.platform.toLowerCase() == "android") { app.log('Filesystem', 'Detected android device...'); if(window.saveInMusic && window.musicPath){ app.log('Filesystem', 'Selected storage persistent SD'); window.resolveLocalFileSystemURL(window.musicPath, app.gotFS, app.fsFail); } else { app.log('Filesystem', 'Selected storage persistent Internal'); window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, app.gotFS, app.fsFail); } } else { app.log('Filesystem', 'No device detected...'); app.log('Filesystem', 'Selected storage DEFAULT (sandbox)'); window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, app.gotFS, app.fsFail); } 

不知何故,所有cordova.file.external *目录都为空(如插件md文件中所述),而externalRootDirectory返回“ storage / emulated / 0 / appdir”(应返回sdcard路径)。 我已经检查了config.xml和AndroidManifest.xml中的权限,如下所示:

 <?xml version='1.0' encoding='utf-8'?> <widget id="com.someorg.appname" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> <name>AppName</name> <description> A basic Framework7 template for PhoneGap. </description> <author email="dev@cordova.apache.org" href="http://cordova.io"> Apache Cordova Team </author> <content src="index.html" /> <access origin="*" /> <allow-intent href="http://*/*" /> <allow-intent href="https://*/*" /> <allow-intent href="tel:*" /> <allow-intent href="sms:*" /> <allow-intent href="mailto:*" /> <allow-intent href="geo:*" /> <platform name="android"> <preference name="android-minSdkVersion" value="14" /> <preference name="AndroidPersistentFileLocation" value="Compatibility" /> <preference name="AndroidExtraFilesystems" value="sdcard,cache" /> <allow-intent href="market:*" /> </platform> <platform name="ios"> <allow-intent href="itms:*" /> <allow-intent href="itms-apps:*" /> <preference name="BackupWebStorage" value="none" /> </platform> <preference name="DisallowOverscroll" value="true" /> <plugin name="cordova-plugin-whitelist" spec="~1.2.0" /> <plugin name="cordova-plugin-console" spec="~1.0.1" /> <plugin name="cordova-plugin-statusbar" spec="~1.0.1" /> <plugin name="cordova-plugin-compat" spec="~1.1.0" /> <plugin name="cordova-plugin-device" spec="~1.1.5" /> <plugin name="cordova-plugin-file" spec="~4.3.2" /> <plugin name="cordova-plugin-file-transfer" spec="~1.6.2" /> <plugin spec="https://github.com/ourcodeworld/cordova-ourcodeworld-filebrowser.git#96a57ef" source="git" /> </widget> 

 <?xml version='1.0' encoding='utf-8'?> <manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="com.someorg.appname" xmlns:android="http://schemas.android.com/apk/res/android"> <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true"> <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize"> <intent-filter android:label="@string/launcher_name"> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:label="Our Code World filepicker" android:name="com.ourcodeworld.plugins.filebrowser.DialogShowPicker"> <intent-filter> <action android:name="com.ourcodeworld.plugins.filebrowser.DialogShowPicker" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:label="Filepicker" android:name="com.nononsenseapps.filepicker.FilePickerActivity" android:theme="@style/FilePickerTheme"> <intent-filter> <action android:name="android.intent.action.GET_CONTENT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="25" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> </manifest> 
该应用程序实际上是在寻求安装的这些权限。 我还实现了一种解决方法(folderpicker),以手动选择sdcard路径。 可悲的是,当我尝试我的getDirectory函数失败时。

 gotFS: function(fileSystem){ window.fileSystem = fileSystem; app.log('Filesystem', 'Filesystem got!'); var entry = ""; if (device.platform.toLowerCase() == "android") { entry = fileSystem; } else { entry = fileSystem.root; } app.log('Filesystem', 'Setting up directory...'); entry.getDirectory(window.appRootDirName, { create: true, exclusive: false }, app.dirReady, app.fsFail); }, 

我已经搜索过很多stackoverflow页面和文档,但是我没有提出任何解决方案。 我已经安装了最新的cordova 6.5,并且还将所有插件更新为最新的稳定版本。 我很乐意提供任何建议。

更新:更清楚地说,应用程序的用户应该能够像以前那样使用下载的媒体文件,而不是通过应用程序来管理和打开它们。

看看这里 插件cordova-plugin-file-opener2解决了这个问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM