Titanium JIRA Archive
Appcelerator Community (AC)

[AC-4961] TiFileProxy: java.io.FileNotFoundException: /storage/emulated/0/com.example.sample/sample.txt: open failed: EACCES (Permission denied)

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionFixed
Resolution Date2017-05-15T14:45:24.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsandroid, requestPermission
ReporterJignesh Kasundra
AssigneeShak Hossain
Created2017-05-12T06:35:10.000+0000
Updated2017-05-17T11:33:00.000+0000

Description

I am trying ro write/read file into the external storage device. but getting the (Permission denied) error. I have already applied run time request permission. OSX, Titanium SDK 6.0.4 , Android Platform 6.0 , Android API 23 ===============
[ERROR] :  TiFileProxy: (main) [1353,1353] 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.V8Runtime.nativeRunModule(Native Method)
[ERROR] :  TiFileProxy: 	at org.appcelerator.kroll.runtime.v8.V8Runtime.doRunModule(V8Runtime.java:196)
[ERROR] :  TiFileProxy: 	at org.appcelerator.kroll.KrollRuntime.runModule(KrollRuntime.java:241)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.TiLaunchActivity.loadActivityScript(TiLaunchActivity.java:128)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.TiLaunchActivity.windowCreated(TiLaunchActivity.java:183)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.TiRootActivity.windowCreated(TiRootActivity.java:172)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.TiBaseActivity.onCreate(TiBaseActivity.java:682)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.TiLaunchActivity.onCreate(TiLaunchActivity.java:169)
[ERROR] :  TiFileProxy: 	at org.appcelerator.titanium.TiRootActivity.onCreate(TiRootActivity.java:161)
[ERROR] :  TiFileProxy: 	at android.app.Activity.performCreate(Activity.java:6237)
[ERROR] :  TiFileProxy: 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
[ERROR] :  TiFileProxy: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
[ERROR] :  TiFileProxy: 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
[ERROR] :  TiFileProxy: 	at android.app.ActivityThread.-wrap11(ActivityThread.java)
[ERROR] :  TiFileProxy: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
[ERROR] :  TiFileProxy: 	at android.os.Handler.dispatchMessage(Handler.java:102)
[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: 	... 26 more

Comments

  1. Jignesh Kasundra 2017-05-12

    Sample Code ============== 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 localPath = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory).nativePath;
       									
       					var sampletxtvar = Ti.Filesystem.getFile(localPath, "sample.txt");
       					sampletxtvar.write("this is my sample text");
       
       					var content = "";		
       
       					if (sampletxtvar.exists()) {
       								content = nomediatxt.read().text;
       								alert("Content is ::: " +content);
       					}
       
    =============== 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>  
       
  2. Sharif AbuDarda 2017-05-12

    Hello, My understanding is you are trying to access the external storage and write there without allowing the permission during runtime. So, what you need to do is, you need to put your code of writing inside a button eventlistner and on window open you allow the access and click on the button to write and so forth. Now, your code works on second run on my device cause i allowed the permission on first run and the app remembers it. Thanks.
  3. Jignesh Kasundra 2017-05-13

    Hi , thanks! it's working on second run, I am able to read and write the file in external storage. but *how can I make it work on first run*. because even after I had read/write file on button click eventListener still not working first run and it throws error while click on button >>>TiFileProxy: java.io.FileNotFoundException: /storage/emulated/0/com.example.sample/sample.txt: open failed: EACCES (Permission denied) NOTE : I have also added requestPermission under window.open event
  4. Sharif AbuDarda 2017-05-15

    Hello, Take a look at my code below.
       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);
       
       });
       
    this works on first run. Thanks.
  5. Jignesh Kasundra 2017-05-17

    Hello, After using above code the issue still exist. first time app launch while click on write button , it throws same error. second app run it works fine. please can you check this.

JSON Source