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

纵饮孤独 提交于 2019-12-06 08:18:53

问题


I am working to develop an android APP with cordova,I wanted to create an folder everyday and store a txt file in it. 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,

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.

I have tried all these below paths for /mnt and /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. 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.


回答1:


From Android 5.0 onwards, the location of the external (removable) SD is no longer a fixed path. Instead, the serial number of the SD card is used in the path. 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/.

In addition, the root of the external SD card (e.g. /storage/4975-1401/) is now read-only to Android apps. This means if your app needs to write to the SD card, it must do so in the application sandbox directory (e.g. /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/.

However, you can use the getExternalSdCardDetails() of cordova-diagnostic-plugin to retrieve the filepaths to the external (removable) SD card, for example:

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. You can use requestRuntimePermission() in cordova-diagnostic-plugin to request this permission.

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.
  • You also need to include <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in the AndroidManifest.xml - see Android permissions.


来源:https://stackoverflow.com/questions/45770040/external-storage-path-sd-card-for-android-5-1-1-and-later-with-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!