[TIMOB-20208] Android 6.0: Issues with writing to file
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | Critical |
| Status | Closed |
| Resolution | Duplicate |
| Resolution Date | 2016-04-21T02:16:47.000+0000 |
| Affected Version/s | n/a |
| Fix Version/s | n/a |
| Components | Android |
| Labels | Android |
| Reporter | Jacob Budin |
| Assignee | Eric Merriman |
| Created | 2015-12-21T17:28:47.000+0000 |
| Updated | 2017-03-22T22:24:14.000+0000 |
Description
*Steps to reproduce:*
1. Add the code below to controllers/index.js:
function doClick(e) {
var file = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, "test.txt");
file.write("i love writing to files");
}
$.index.open();
2. Launch the app.
3. Click on the "Hello, World" text.
*Results:*
12-21 12:15:24.350: E/TiBlob(12960): (KrollRuntimeThread) [19,12681] /storage/emulated/0/com.androidwritefailure/test.txt: open failed: ENOENT (No such file or directory)
12-21 12:15:24.350: E/TiBlob(12960): java.io.FileNotFoundException: /storage/emulated/0/com.androidwritefailure/test.txt: open failed: ENOENT (No such file or directory)
12-21 12:15:24.350: E/TiBlob(12960): at libcore.io.IoBridge.open(IoBridge.java:452)
12-21 12:15:24.350: E/TiBlob(12960): at java.io.FileInputStream.<init>(FileInputStream.java:76)
12-21 12:15:24.350: E/TiBlob(12960): at org.appcelerator.titanium.io.TiFile.getInputStream(TiFile.java:264)
12-21 12:15:24.350: E/TiBlob(12960): at org.appcelerator.titanium.TiBlob.getInputStream(TiBlob.java:389)
12-21 12:15:24.350: E/TiBlob(12960): at org.appcelerator.titanium.TiBlob.guessContentTypeFromStream(TiBlob.java:209)
12-21 12:15:24.350: E/TiBlob(12960): at org.appcelerator.titanium.TiBlob.loadBitmapInfo(TiBlob.java:270)
12-21 12:15:24.350: E/TiBlob(12960): at org.appcelerator.titanium.TiBlob.blobFromFile(TiBlob.java:140)
12-21 12:15:24.350: E/TiBlob(12960): at org.appcelerator.titanium.TiBlob.blobFromFile(TiBlob.java:113)
12-21 12:15:24.350: E/TiBlob(12960): at org.appcelerator.titanium.io.TiFile.read(TiFile.java:351)
12-21 12:15:24.350: E/TiBlob(12960): at org.appcelerator.titanium.TiFileProxy.read(TiFileProxy.java:239)
*Notes:*
The code is working in Android 5.1 and Android 5.0.
This has to do with Storage permission required in Android 6.0. Workaround for now is to write it this way:-
This will provide the storage permission needed.if (Ti.Media.hasCameraPermissions()) { var f = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, "tests.txt"); f.write('writing to the file would be enough to create it'); } else { Ti.Media.requestCameraPermissions(function(e) { if (e.success === true) { var f = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, "tests.txt"); f.write('writing to the file would be enough to create it'); } else { alert("Access denied, error: " + e.error); } }); }https://github.com/gimdongwoo/Ti-Android-RequestStoragePermission this plugin should request only the storage permission without the camera permission.
Should be fixed by this PR: https://github.com/appcelerator/titanium_mobile/pull/7948 You can use:
var storagePermission = "android.permission.READ_EXTERNAL_STORAGE"; var hasStoragePerm = Ti.Android.hasPermission(storagePermission); var permissionsToRequest = []; if (!hasStoragePerm) { permissionsToRequest.push(storagePermission); } if (permissionsToRequest.length > 0) { Ti.Android.requestPermissions(permissionsToRequest, function(e) { if (e.success) { Ti.API.info("SUCCESS"); } else { Ti.API.info("ERROR: " + e.error); } }); }Thanks [~michael] Resolving this ticket as there are tickets that have solved the issue such as TIMOB-20320
Closing ticket as duplicate and links to the related ticket have been provided above.