授予 android 6.0 应用使用 appcelerator 写入内部存储的权限的工作代码

Working code for giving android 6.0 app permission to write to internal storage with appcelerator

我花了三天时间尝试解决这个问题,尝试了许多我能找到的不同代码,但结果为零。任何人都知道如何让 android 6.0 设备 permission 中的 android 应用程序 运行 使用 appcelerator 写入内部存储? SDK 5.4.0 或 5.3.1

我正在使用这个模块,这实际上是一种解决方法: https://github.com/gimdongwoo/Ti-Android-RequestStoragePermission

好的,在 appcelerator 的帮助下,我似乎终于有了工作代码示例。

在index.js

function doClick(e) {
if (!Ti.Filesystem.hasStoragePermissions()) {
Ti.Filesystem.requestStoragePermissions(function(result) {
  console.log('Permission granted? ' + result.success);
  if (result.success) {
    getFile();
  }
  else {
    alert('Permission denied.');
  }
});
  }
else {
getFile();
}
}
function getFile() {
  var url = "http://www.appcelerator.com/wp-content/uploads/GettingStartedTitanium_Linux.pdf";
var fileName = "GettingStartedTitanium_Linux.pdf";

var httpClient = Ti.Network.createHTTPClient();
httpClient.onerror = function(e) {
alert('Download Error: ' + e.error);
};
httpClient.onload = function(e) {
var filePath = Ti.Filesystem.tempDirectory + Ti.Filesystem.separator + fileName;
var f = Ti.Filesystem.getFile(filePath);
var file = f.write(this.responseData);
alert('File exist? ' + f.exists());
};
httpClient.open('GET', url);
httpClient.send();
}
$.index.open();

在"index.xml"

<Alloy>
<Window class="container">
<Label id="label" onClick="doClick">Hello, World</Label>
</Window>
</Alloy>

在"Tiapp.xml"Android部分

<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
</android>