Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-26370

Android: "Failed to create external storage directory" when download image and save to camera roll

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: Release 8.0.0
    • Component/s: None
    • Environment:

      TI SDK 7.3.1 (fails below as well). MacOS. Android API 26, 27 & 28

      Description

      When downloading any image, saving to temp folder and then attempting to save the image to the Android Camera Roll, results in error (saving to Camera Roll) "TiMedia: (main) [526999,526999] Failed to create external storage directory". Works as expected on Android API levels 25 and below. Fails on API levels 26, 27, 28. Tested on emulators API 25-28 and devices API 21 and API 28.

      Sample Test Code:

      var checkCameraPermissions = function(callback) {
          if (Ti.Media.hasCameraPermissions()) {
              Ti.UI.createAlertDialog({ cancel: 0, buttonNames: ['OK'], title:'Permissions', message:'Already had permissions. Good to go.'}).show();
              callback(true);
          } else { 
              Ti.Media.requestCameraPermissions(function(e) {
                   if (e.success === true) {
                       Ti.UI.createAlertDialog({ cancel: 0, buttonNames: ['OK'], title:'Permissions', message:'User granted permissions. Good to go.'}).show();
                       callback(true);
                   } else {
                       Ti.UI.createAlertDialog({ cancel: 0, buttonNames: ['OK'], title:'Permissions', message:'User did not granted permissions. Bad day.'}).show();
                       callback(false);
                   }
              });
          }
      };
        
      var win = Ti.UI.createWindow({
          exitOnClose: true,
          fullscreen: false,
          title: 'Test'
      });
       
      var button = Titanium.UI.createButton({
          title: 'Test DL Img & Save to Camera Roll'
      });   
      button.addEventListener('click', function(e) {
          checkCameraPermissions(function(returnedData) {            
              if (returnedData == true) {
                  var xhrImageGet = Ti.Network.createHTTPClient({
                      onerror : function(xhrErr) {
                          Ti.UI.createAlertDialog({ cancel: 0, buttonNames: ['OK'], title:'Error', message:'Unable to download the remote image. ' + xhrErr.error}).show();
                      },
                      onload : function() {
                          try {
                              var image_to_save = this.responseData;                    
                              var picture = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory, 'icon-arrowFeature.png');
                              picture.write(image_to_save);                                                        
                              Ti.Media.saveToPhotoGallery(picture.read(), {
                                  success:function(){
                                      Ti.UI.createAlertDialog({ cancel: 0, buttonNames: ['OK'], title:'Image Saved', message:'icon-arrowFeature.png has been saved to your photo gallery.'}).show();
                                  },
                                  error:function(err){
                                      Ti.UI.createAlertDialog({ cancel: 0, buttonNames: ['OK'], title:'Error Saving to Gallery', message:err.error}).show();
      // FAILS RIGHT HERE WITH NO ERROR MESSAGE RETURNED. HOWEVER CONSOLE SHOWS THE ERROR TiMedia: (main) [526999,526999] Failed to create external storage directory.
                                  }
                              });                        
                              image_to_save = null;
                          }
                          catch(saveErr) {
                              Ti.UI.createAlertDialog({ cancel: 0, buttonNames: ['OK'], title:'Error', message:'Unable to save the file to your device. ' + saveErr.error}).show();
                          }
                      }
                  });                                        
                  xhrImageGet.open("GET", 'https://s3.amazonaws.com/www.appcelerator.com.images/icon-arrowFeature.png');
                  xhrImageGet.send();                                            
              } else {
                  Ti.UI.createAlertDialog({ cancel: 0, buttonNames: ['OK'], title:'Permission Not Granted', message:'You have not granted the app the needed permissions to access your camera and/or device storage.'}).show();
              }        
          });                                            
      });
       
      win.add(button);        
      win.open();
      

      Test App tiapp.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <ti:app xmlns:ti="http://ti.appcelerator.org">
          <id>com.walkthelot.testsavetogallery</id>
          <name>TestSaveToGallery</name>
          <version>1.0</version>
          <publisher>Ken</publisher>
          <url/>
          <description>undefined</description>
          <copyright>2018 by Ken</copyright>
          <icon>appicon.png</icon>
          <fullscreen>false</fullscreen>
          <navbar-hidden>false</navbar-hidden>
          <analytics>true</analytics>
          <guid>a17321c0-c4e2-4542-9222-e3f5731f8fb5</guid>
          <property name="ti.ui.defaultunit" type="string">dp</property>
          <property name="run-on-main-thread" type="bool">true</property>
          <android xmlns:android="http://schemas.android.com/apk/res/android">
              <manifest android:installLocation="auto">
                  <uses-permission android:name="android.permission.WAKE_LOCK"/>
                  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
                  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
                  <uses-permission android:name="android.permission.INTERNET"/>
                  <uses-permission android:name="android.permission.VIBRATE"/>
                  <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"/>
                  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
                  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
                  <uses-permission android:name="android.permission.CAMERA"/>
                  <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="26"/>
              </manifest>
          </android>
          <modules/>
          <deployment-targets>
              <target device="android">true</target>
              <target device="ipad">false</target>
              <target device="iphone">false</target>
          </deployment-targets>
          <sdk-version>7.3.1.GA</sdk-version>
      </ti:app>
      

      I've dug and dug and can not find anything that I may be missing.

        Attachments

          Activity

            People

            • Assignee:
              ybanev Yordan Banev
              Reporter:
              ken@walkthelot.com Ken Rucker
              Reviewer:
              Joshua Quick
            • Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Backbone Issue Sync

                • Titanium SDK/CLI <> Titanium Mobile
                  Synced with:
                  TIMOB-6569
                  Sync status:
                  ERROR
                  Last received:
                  Last sent:

                  Git Source Code