how do I open a newly written file from applicatio

2019-09-10 10:26发布

Im working on an app (flex 4.12 sdk, using flashbuilder 4.5, creating an app for ios and android, testing on an android htc one primarily)... and am using the camera to capture a file... Im then saving that image to the application storage directory, and I want to open the image in the default web browser or trigger a native dialog (android users) to choose the web browser of their choice... how it opens isnt really important right now -- Im mainly trying to just 'access' it with the device and 'load' it outside my air app...

heres the code I have:

                var fs2 : FileStream = new FileStream();
                fs2.addEventListener(Event.CLOSE, fileCompleteHandler); 
                var targetFile : File = File.applicationStorageDirectory.resolvePath("test.jpg");
                fs2.openAsync(targetFile, FileMode.WRITE);
                fs2.writeBytes(myBMDByteArray,0,myBMDByteArray.length);
                fs2.close();

and for the event listener that detects the close of the newly created file:

                function fileCompleteHandler(e:Event):void {
                    trace('File saved.');
                    trace('exists? ' + targetFile.exists);
                    trace('the url: ' + targetFile.url);
                    trace('path: ' + targetFile.nativePath);
                    navigateToURL(new URLRequest(targetFile.url));
                }

I get the following info back from this listener

  • File saved.
  • exists? true
  • the url: app-storage:/test.jpg
  • path: /data/data/air.com.xxxxx.apptesting.debug/com.xxxxx.apptesting.debug/Local Store/test.jpg

... and problem is that navigateToURL cant access the location where the file is stored (the protocol shows in browser as file:///data/data/air.com/xxx... )

how can I use navigateToURL to get access to this newly created file in the web browser or whatever native application the device associates with the file (its a .JPG file)? I also have had success in adding the newly created image to the camera roll but couldnt figure out how to then open that newly saved image in the native camera roll or whatever app the device chooses or presents to the user for the .jpg format.

I can show the user the image INSIDE my app by referencing the bitmap data fine, I just want to give the user access to the physical file that Im creating on their device.

I even have had success in posting (via urlLoader) the bitmap data as base64 encoding and then creating a file on the server side and loading that url but the encoding and trip to and from the server to give the user the image adds a lot of overhead and it takes a little too long and I'd like to avoid that elongated process.

Thanks for any help anyone can provide - let me know if I need to be more specific in any of this.

4条回答
相关推荐>>
2楼-- · 2019-09-10 11:08

Solved the issue... I was able to store / write my file in the documentsDirectory using:

var targetFile : File = File.documentsDirectory.resolvePath('test.jpg');

and then

navigateToURL(new URLRequest(targetFile.url));

And this works fine now. Hopefully it helps someone else! Seems that the storage directory SHOULD work but up until now I've only written to and read files stored there... maybe to open the files one HAS to copy it to a 'safe' location in the filesystem (i.e. sd card?)... will move on to test in ios Now - hope all works well in that OS. Thanks all who chimed in on this.

查看更多
Fickle 薄情
3楼-- · 2019-09-10 11:13

The applicationStorageDirectory can only be accessed by the application it belongs too when using Android or iOS. navigateToURL() hands over your request to the default browser, which cannot access said directory.

documentsDirectory is a public directory in Android, but not in iOS. So it cannot be used for both platforms. Unfortunately none of the pre-compiled file paths File has point to a public directory in iOS. You can find a table explaining all the pre-compiled paths here

查看更多
神经病院院长
4楼-- · 2019-09-10 11:18

On your application.xml you need this permissions set inside android > manifestAdditions > manifest:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

With this permissions you can save files to applicationStorageDirectory:

const FILE_LOADER:URLLoader = new URLLoader();
FILE_LOADER.addEventListener(Event.COMPLETE, onTempFileComplete);
FILE_LOADER.dataFormat = URLLoaderDataFormat.BINARY;
FILE_LOADER.load(new URLRequest(BASE_URL + filePath));
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-09-10 11:19

My first hunch is that you need to specify the proper user-permissions in your application descriptor so you can use the openWith functionality with content from your application.

Remember that you need to specify this for IOS and Android specifically.

查看更多
登录 后发表回答