Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-20208] Android 6.0: Issues with writing to file

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionDuplicate
Resolution Date2016-04-21T02:16:47.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
LabelsAndroid
ReporterJacob Budin
AssigneeEric Merriman
Created2015-12-21T17:28:47.000+0000
Updated2017-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.

Comments

  1. Ashraf Abu 2016-01-08

    This has to do with Storage permission required in Android 6.0. Workaround for now is to write it this way:-
          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);
                       }
              });
          }
       
    This will provide the storage permission needed.
  2. Michael Gangolf 2016-01-08

    https://github.com/gimdongwoo/Ti-Android-RequestStoragePermission this plugin should request only the storage permission without the camera permission.
  3. carlo 2016-01-26

  4. Michael Gangolf 2016-04-20

    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);
               }
           });
       }
       
  5. Ashraf Abu 2016-04-21

    Thanks [~michael] Resolving this ticket as there are tickets that have solved the issue such as TIMOB-20320
  6. Lee Morris 2017-03-22

    Closing ticket as duplicate and links to the related ticket have been provided above.

JSON Source