Titanium JIRA Archive
Appcelerator Community (AC)

[AC-4969] open failed: EACCES (Permission denied)

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionCannot Reproduce
Resolution Date2017-05-29T13:17:23.000+0000
Affected Version/sAppcelerator Studio 4.5.0
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsn/a
ReporterJignesh Kasundra
AssigneeShak Hossain
Created2017-05-18T06:08:25.000+0000
Updated2017-05-29T13:17:23.000+0000

Description

I am trying to write/read file into the external storage, Android 6.0 , on first app launching request permission for external storage not working(request Permission already allowed). however if I fully close the application and re-launch it then it's work, how can make it work on first run =================================
[ERROR] :  TiFileProxy: (main) [51879,60031] IOException encountered
[ERROR] :  TiFileProxy: java.io.FileNotFoundException: /storage/emulated/0/com.example.sample/sample.txt: open failed: EACCES (Permission denied)
[ERROR] :  TiFileProxy: 	at libcore.io.IoBridge.open(IoBridge.java:452)
[ERROR] :  TiFileProxy: 	at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.io.TiFile.getOutputStream(TiFile.java:273)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.io.TiFile.open(TiFile.java:336)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.io.TiFile.write(TiFile.java:460)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.TiFileProxy.write(TiFileProxy.java:287)
[ERROR] :  TiFileProxy: 	at org.appcelerator.kroll.runtime.v8.V8Object.nativeFireEvent(Native Method)
[ERROR] :  TiFileProxy: 	at org.appcelerator.kroll.runtime.v8.V8Object.fireEvent(V8Object.java:62)
[ERROR] :  TiFileProxy: 	at org.appcelerator.kroll.KrollProxy.doFireEvent(KrollProxy.java:872)
[ERROR] :  TiFileProxy: 	at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1095)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:357)
[ERROR] :  TiFileProxy: 	at android.os.Handler.dispatchMessage(Handler.java:98)
[ERROR] :  TiFileProxy: 	at android.os.Looper.loop(Looper.java:148)
[ERROR] :  TiFileProxy: 	at android.app.ActivityThread.main(ActivityThread.java:5417)
[ERROR] :  TiFileProxy: 	at java.lang.reflect.Method.invoke(Native Method)
[ERROR] :  TiFileProxy: 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
[ERROR] :  TiFileProxy: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
[ERROR] :  TiFileProxy: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
[ERROR] :  TiFileProxy: 	at libcore.io.Posix.open(Native Method)
[ERROR] :  TiFileProxy: 	at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
[ERROR] :  TiFileProxy: 	at libcore.io.IoBridge.open(IoBridge.java:438)
[ERROR] :  TiFileProxy: 	... 16 more

Comments

  1. Jignesh Kasundra 2017-05-18

    sample code for the above issue (app.js) =============
       
       var cameraPermission = "android.permission.CAMERA";
        
       var storagePermission = "android.permission.READ_EXTERNAL_STORAGE";
        
       var hasCameraPermission = Ti.Android.hasPermission(cameraPermission);
        
       var hasStoragePermission = Ti.Android.hasPermission(storagePermission);
        
       var permissionsToRequest = [];
        
       var win = Ti.UI.createWindow({
        
           title : 'mywin',
        
           backgroundColor : '#fff',
        
           width : Ti.UI.FILL,
        
           height : Ti.UI.FILL
        
       });
        
       win.open();
        
       if (!hasCameraPermission) {
        
           permissionsToRequest.push(cameraPermission);
        
       }
        
       if (!hasStoragePermission) {
        
           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);
        
               }
        
           });
        
       }
        
       var btn1 = Ti.UI.createButton({
        
           title : 'Write',
           top : 60
        
       });
       win.add(btn1);
        
       btn1.addEventListener('click', function() {
           var localPath = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory).nativePath;
        
           var sampletxtvar = Ti.Filesystem.getFile(localPath, "sample.txt");
        
           sampletxtvar.write("this is my sample text");
        
           alert(sampletxtvar.read().text);
        
       }); 
       
  2. Sharif AbuDarda 2017-05-18

    Hello, This is working for me with Android 6.0.1.device. On first run I am allowing the Camera and Storage permission that pops up right after the app opens. Than when the button is clicked the alert dialog opens with "this is my sample text" alert. From now on clicking on the button shows the alert. If I close the app and reopen, this time there is no permission asked to allowed and clicking on the button shows the alert. If I close the app and remove the permissions for that app from the device application manager. I can open the app and the permission dialog pops up now after app opens. If I allow them, than the button click shows the alert text. I am testing with SDK 6.0.4.GA. Make sure you are allowing the permissions on first run before clicking on the button.
  3. Sharif AbuDarda 2017-05-18

    Hello, can you try this with the write permission?
       
       var storePermission = "android.permission.WRITE_EXTERNAL_STORAGE";
       
       var storagePermission = "android.permission.READ_EXTERNAL_STORAGE";
       
       var hasStorePermission = Ti.Android.hasPermission(storePermission);
       
       var hasStoragePermission = Ti.Android.hasPermission(storagePermission);
       
       var permissionsToRequest = [];
       
       var win = Ti.UI.createWindow({
       
           title : 'mywin',
       
           backgroundColor : '#fff',
       
           width : Ti.UI.FILL,
       
           height : Ti.UI.FILL
       
       });
       
       win.open();
       
       if (!hasStorePermission) {
       
           permissionsToRequest.push(storePermission);
       
       }
       
       if (!hasStoragePermission) {
       
           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);
       
               }
       
           });
       
       }
       
       var btn1 = Ti.UI.createButton({
       
           title : 'Write',
       
           top : 60
       
       });
       
       win.add(btn1);
       
       
       
       btn1.addEventListener('click', function() {
       
           var localPath = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory).nativePath;
       
           var sampletxtvar = Ti.Filesystem.getFile(localPath, "sample.txt");
       
           sampletxtvar.write("this is my sample text");
       
           alert(sampletxtvar.read().text);
       
       
       });
       
    Use this in tiapp.xml
       <manifest>
       
                   <uses-permission android:name="android.permission.INTERNET"/>
       
                   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
       
                   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
       
                   <application android:hardwareAccelerated="true" android:largeHeap="true"/>
       
       </manifest>  
       
  4. Aminul Islam 2017-05-18

    Hi, I have tested following test code with latest SDK 6.0.4.GA on android 6.0.1(HTC one m 8). It's working as expected. On the first run, I am allowing the storage permission that pops up right after the app opens. Then when the button is clicked the alert dialog opens with "this is my sample text" alert. If I close the app and reopen, this time there is no permission asked to allowed and clicking on the button shows the alert.
       var storePermission = "android.permission.WRITE_EXTERNAL_STORAGE";
        
       var storagePermission = "android.permission.READ_EXTERNAL_STORAGE";
        
       var hasStorePermission = Ti.Android.hasPermission(storePermission);
        
       var hasStoragePermission = Ti.Android.hasPermission(storagePermission);
        
       var permissionsToRequest = [];
        
       var win = Ti.UI.createWindow({
        
           title : 'mywin',
        
           backgroundColor : '#fff',
        
           width : Ti.UI.FILL,
        
           height : Ti.UI.FILL
        
       });
        
       win.open();
        
       if (!hasStorePermission) {
        
           permissionsToRequest.push(storePermission);
        
       }
        
       if (!hasStoragePermission) {
        
           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);
        
               }
        
           });
        
       }
        
       var btn1 = Ti.UI.createButton({
        
           title : 'Write',
        
           top : 60
        
       });
        
       win.add(btn1);
        
        
        
       btn1.addEventListener('click', function() {
        
           var localPath = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory).nativePath;
        
           var sampletxtvar = Ti.Filesystem.getFile(localPath, "sample.txt");
        
           sampletxtvar.write("this is my sample text");
        
           alert(sampletxtvar.read().text);
        
        
       });
       
    Test Environment
       Appcelerator Command-Line Interface, version 6.2.1
       Operating System
         Name                        = Mac OS X
         Version                     = 10.11.6
         Architecture                = 64bit
         # CPUs                      = 4
         Memory                      = 8589934592
       Node.js
         Node.js Version             = 4.4.4
         npm Version                 = 2.15.1
       Titanium CLI
         CLI Version                 = 5.0.13
       Titanium SDK
         SDK Version                 = 6.0.4.GA
         SDK Path                    = /Users/Raju/Library/Application Support/Titanium/mobilesdk/osx/6.0.4.GA
         Target Platform             = android
       
  5. Jignesh Kasundra 2017-05-19

    Hello, I am still facing same issue on first run after tryed with write permission, I am checking this on emulator android version 6.0, not tested on real android device version 6.x
  6. Sharif AbuDarda 2017-05-22

    Please test this on device. Let us know the version you are testing.

JSON Source