简体   繁体   English

适用于Android 5.1.1及更高版本的Cordova的外部存储路径(SD卡)

[英]External Storage Path (SD card) for Android 5.1.1 and later with Cordova

I am working to develop an android APP with cordova,I wanted to create an folder everyday and store a txt file in it. 我正在开发一个带有cordova的android APP,我想每天创建一个文件夹并在其中存储一个txt文件。 everything I tried is working for the internal memory of each android but not for the External SD card, have a look and help me out, 我尝试的一切都是为每个机器人的内部存储器而不是外部SD卡工作,请看看并帮助我,

 if(sDeviceVersion=='4.0' || sDeviceVersion=='4.0.4'){ var sPath = 'file:///storage/extSdCard/'; }else if(sDeviceVersion=='4.1' || sDeviceVersion=='4.1.2' ||sDeviceVersion=='4.3.1'){ var sPath = 'file:///storage/extSdCard/'; }else if(sDeviceVersion=='4.4' || sDeviceVersion=='4.4.4'){ var sPath = 'file:///storage/extSdCard/'; }else if(sDeviceVersion=='5.0' || sDeviceVersion=='5.1.1'){ var sPath = 'file:///mnt/sdcard/'; // }else if(sDeviceVersion=='6.0' || sDeviceVersion=='6.0.1'){ var sPath = 'file:///storage/sdcard1/'; }else if(sDeviceVersion=='7.0' || sDeviceVersion=='7.1.2'){ var sPath = 'file:///storage/sdcard1/'; } 

Above condition is working till 4.4.4 version, after 5.0 the PATH is not correct. 以上条件工作到4.4.4版本,5.0之后PATH不正确。

I have tried all these below paths for /mnt and /storage 我已经为/ mnt/ storage尝试了以下所有这些路径

// sdcard0 works on all the androind for Internal Memory
// 'file:///storage/sdcard1/'; suppose to work for external in higher version but 
// 'file:///mnt/sdcard/'; it works but in Internal memory ERROR
// externalSdCard             ----->   Not found with mnt and storage
// SECONDARY_STORAGE
// 'file:///storage/UsbDriveB/'  -----------> didn't worked
// external_sd is not worked with storage and mnt

I read everywhere that sdcard0 is Internal and sdcard1 is an External, but it doesn't seems to be working anymore. 我到处都读到sdcard0是内部的,而sdcard1是外部的,但它似乎不再起作用了。 can anybody help me with the Path. 任何人都可以帮我路径。

Even I have tried 即使我尝试过

 alert(cordova.file.externalRootDirectory); // file:///storage/sdcard0/ Internal memory alert(cordova.file.externalApplicationStorageDirectory); // path to file:///android/data... alert(cordova.file.dataDirectory); // file:///data/androind/data/... alert(cordova.file.externalDataDirectory); // file://storage/sdcard0/android/data/... 

all above is working for Internal storage only. 以上所有仅适用于内部存储。

All the permission for STORAGE/READ/WRITE to external storage is given. 给出了对外部存储器的STORAGE / READ / WRITE的所有许可。

From Android 5.0 onwards, the location of the external (removable) SD is no longer a fixed path. 从Android 5.0开始,外部(可移动)SD的位置不再是固定路径。 Instead, the serial number of the SD card is used in the path. 而是在路径中使用SD卡的序列号。 For example, on my Samsung Galaxy S4 which is running Android 7.1.1, the physical external removable SD card path is /storage/4975-1401/ . 例如,在运行Android 7.1.1的Samsung Galaxy S4上,物理外部可移动SD卡路径为/storage/4975-1401/

In addition, the root of the external SD card (eg /storage/4975-1401/ ) is now read-only to Android apps. 此外,外部SD卡的根目录(例如/storage/4975-1401/ )现在对Android应用程序是只读的。 This means if your app needs to write to the SD card, it must do so in the application sandbox directory (eg /storage/4975-1401/Android/data/your.app.package.id/files ). 这意味着如果您的应用需要写入SD卡,则必须在应用程序沙箱目录中执行此操作(例如/storage/4975-1401/Android/data/your.app.package.id/files )。

cordova-plugin-file does not enable you to access the external (removable) SD card: for example cordova.file.externalRootDirectory returns file:///storage/emulated/0/ . cordova-plugin-file不允许您访问外部(可移动)SD卡:例如cordova.file.externalRootDirectory返回file:///storage/emulated/0/

However, you can use the getExternalSdCardDetails() of cordova-diagnostic-plugin to retrieve the filepaths to the external (removable) SD card, for example: 但是,您可以使用cordova-diagnostic-plugingetExternalSdCardDetails()来检索外部(可移动)SD卡的文件路径,例如:

function getExternalSdLocation(done){
    cordova.plugins.diagnostic.getExternalSdCardDetails(function(details){
        details.forEach(function(detail){
            if(detail.type == "application"){
                cordova.file.externalSdCardApplicationDirectory = detail.filePath;
            }else if(detail.type == "root"){
                cordova.file.externalSdCardRootDirectory = detail.filePath;
            }
        });
        done();
    }, function(error){
        console.error(error);
        done();
    });
}

getExternalSdLocation(function(){
    // use cordova.file.externalSdCardApplicationDirectory to write to SD card
});

For Android 6.0 and above, run-time permission is required in order to access the external SD card. 对于Android 6.0及更高版本,需要运行时权限才能访问外部SD卡。 You can use requestRuntimePermission() in cordova-diagnostic-plugin to request this permission. 您可以使用cordova-diagnostic-plugin中的 requestRuntimePermission()来请求此权限。

function requestExternalSdPermission(done){
    cordova.plugins.diagnostic.requestRuntimePermission(function(status){
        switch(status){
            case cordova.plugins.diagnostic.permissionStatus.GRANTED:
                console.log("Permission granted");
                getExternalSdLocation(done);
                break;
            case cordova.plugins.diagnostic.permissionStatus.DENIED:
                console.log("Permission denied");
                askAgain(done);
                break;
            case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
                console.log("Permission permanently denied");
                reportError(done);
                break;
        }
    }, function(error){
        console.error("The following error occurred: "+error);
        reportError(done);
    }, cordova.plugins.diagnostic.permission.WRITE_EXTERNAL_STORAGE);
}
  • Running this code on Android 5 and below will alwys result in "permission granted" without requesting permission from the user. 在Android 5及更低版本上运行此代码将导致“授予权限”而无需用户的许可。
  • You also need to include <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in the AndroidManifest.xml - see Android permissions . 您还需要在AndroidManifest.xml包含<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> - 请参阅Android权限

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

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