Error when save BitmapImage to Local folder Windows Phone 8.1 - windows-phone-8.1

I have BitmapImage (downloaded from live sdk output), and i do following code to save to local folder
var rass = RandomAccessStreamReference.CreateFromUri(new Uri(photoResultdyn.location, UriKind.Absolute));
var stream = await rass.OpenReadAsync();
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
And use WritableBitmap to convert bitmapImage to a WritableBitMap image.
WritableBitmap wbm = new WritableBitmap(bitmapImage);
but failed.
any solutions ?
I am using Windows Phone 8.1 (In Universal App Solution)

The WriteableBitmap(BitmapImage) constructor is for Silverlight. It is not available to Windows Runtime apps.
To save your stream to disk along with adding it to a BitmapImage, you can save the original stream that you create the BitmapImage directly to disk. This will avoid decoding and reencoding the bitmap, which (depending on the image file format) may be lossy.
If the save is far enough away from the download that you don't have the stream then you can avoid converting from BitmapImage by using a WriteableBitmap to begin with. Both are BitmapSources and can be used interchangeably in most situations.
var bitmapImage = new WriteableBitmap();
bitmapImage.SetSource(stream);

Related

Adobe Air URLRequest to Local File Works Windows 7, Not 10

I developed an Adobe Air App for a small intranet. All computers have been running Windows 7, but now are beginning to be replaced with Windows 10 systems. I can access the mapped drive "I" and the local "C" drive using the file class on Windows 7 machines, but only the mapped drive "I" on Windows 10.
Edit: Capabilities.localFileReadDisable returns false on both Windows 7 and Windows 10 systems.
****I could bypass the need for the local file if Air could get any specific information about the machine it is running on, serial number, mac address, computer name, etc. It really makes no difference what information I get, it just has to be unique to that computer. And using cookies isn't an option because they are volatile****
The following code accomplishes two things.
First, it displays the running version of the Air file and looks for a file on a mapped drive with the latest version available. If they are the same, the computer is running the latest version. If they aren't the same, the new version is displayed to the user, indicating the app should be updated.
Second, it grabs the name of the specific computer from a text file residing on the local drive. That name is used on reports to indicate which computer was being used. There is probably a far superior way to accomplish this, but on Windows 7, it works perfectly for me. Unfortunately, Windows 10 throws an error when trying to access the file on the local drive.
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: file:///C:/machineName.txt
Any help would be greatly appreciated.
var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
var ns:Namespace = appXML.namespace();
version_txt.text = "V"+appXML.ns::versionNumber;
// Define path to the version number
var updatePath:URLRequest = new URLRequest("file:///I:/air/update.txt");
// Define path to name of specific pc
var machineName:URLRequest = new URLRequest("file:///C:/machineName.txt");
// Define the URLLoaders
var updateLoader:URLLoader = new URLLoader();
function checkUpdate():void{
updateLoader.load(updatePath);
}
var nameLoader:URLLoader = new URLLoader();
function checkName():void{
nameLoader.load(machineName);
}
// Listen for when the file has finished loading.
updateLoader.addEventListener(Event.COMPLETE, loaderComplete);
function loaderComplete(e:Event):void
{
// The output of the text file is available via the data property
// of URLLoader.
if(Number(appXML.ns::versionNumber)<Number(updateLoader.data)){
update_txt.text = "UPDATE TO V"+updateLoader.data;
}
}
nameLoader.addEventListener(Event.COMPLETE, nameComplete);
var name_txt:String = new String;
function nameComplete(e:Event):void{
name_txt = nameLoader.data;
var holder:String = version_txt.text;
version_txt.text = name_txt+" ** "+holder;
}

Flash SWF, reading the file contents from inside the browser, possible?

I have an actionscript project which I deploy as both a Air desktop application and in the browser using the Flash plugin.
My desktop deployment reads the SWF file itself, creates a hash of the file and send it to my server. If the hash does not match whats expected the SWF then uploads itself to my server for further examination. This all uses the flash.filesystem libraries.
Can the same be done when deployed as Flash file in the browser. I do not have access to flash.filesystem for browser deployment.
Does a way other than using filesystem exist to read the SWF itself.
According to this article (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Worker.html) your entire SWF content is accessible via loaderInfo.bytes:ByteArray so there's no need to load anything. Enjoy.
Fallback to loading:
var aRequest:URLRequest = new URLRequest;
// Property loaderInfo.url contains the full path to the SWF.
aRequest.url = loaderInfo.url;
var aLoader:URLLoader = new URLLoader;
// Set data format to binary instead of default text.
aLoader.dataFormat = URLLoaderDataFormat.BINARY;
aLoader.addEventListener(Event.COMPLETE, onReady);
aLoader.load(aRequest);
function onReady(e:Event):void
{
var anSWF:ByteArray = aLoader.data as ByteArray;
trace(anSWF.length);
}
To get the SWF bytes, use URLStream API to receive bytes into some bytearray variable.
You can then use ByteArray API to read (process) the bytearray.
You can deploy your Flex code to an AIR desktop application, which is translated to a SWF file, which can be used in browsers as an embedded object.
Please read more here.

Write to file code works on development, breaks when deployed to the store

I am trying to write a line of text to a text file. On the emulator or running from VS on the device, it works perfectly but when downloaded from the store, this code emits the error:
System.UnauthorizedAccessException: Access to the path
'C:\Data\Programs\{XXXXXX-XXXXX-XXX-XXXXX}\Install\Data\results.csv' is denied.
Here is my code:
var path = "Data/results.csv";
var uri = new Uri(path, UriKind.RelativeOrAbsolute).ToString();
using (var rd = new StreamWriter(uri, true))
{
var line = String.Format("{0};{1}", field1, field2);
rd.WriteLine(line);
rd.Close();
}
Am I doing something wrong? How can the code work on development?
Use:
var iso = IsolatedStorageFile.GetUserStoreForApplication();
It gets the storage that your app can use on users phone. You can then create directories (or files) for example:
iso.CreateDirectory("Data");
Why it works in emulator or on your device? I'd guess because MS does not care about access rights here.

Unauthorized Access Exception when Creating an instance of SpeechSynthesizer in WP8.1 Emulator

I was trying to recreate the simle Text to Speech example used on the MSDN website. However whenever the code came to create the instance of the SpeechSynthesizer class it failed with a Unauthorised Acception error when running on the WP8.1 emulator. I currently do not have an actual device to test on to see if this makes a difference.
My code was simply:
private async void TTS()
{
// The media object for controlling and playing audio.
MediaElement mediaElement = new MediaElement();
// The object for controlling the speech synthesis engine (voice).
var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
// Generate the audio stream from plain text.
SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello World");
// Send the stream to the media object.
mediaElement.SetSource(stream, stream.ContentType);
mediaElement.Play();
}
I know there was an issue with the SpeechSynthesizer in Windows 8.1, and I found solutions to this when looking to fix the problem, but found little about the problem with WP8.1 SpeechSynthesizer. Has anybody else came across this problem and found a fix?
You should add one DeviceCapability in Package.appxmanifest file:
In DeviceCapability Tab, check the microphone, because it will provides access to the microphone’s audio feed, which allows the app to record audio from connected microphones.
Look at this library: App capability declarations (Windows Runtime apps)

Open un-associated files with Adobe Air

I know it is possible to open files with their default application using the openWithDefaultApplication method of the file class. However, when you try to open an un-associated file (perhaps a proprietary or custom extension) using this method, a runtime error is thrown.
Is there anyway to prompt the user to open a file with a particular application like Windows does with it's "Open with..." dialog?
If not using the AIR the framework, via the NativeProcessAPI?
SOLUTION - using Pixel Elephants answer below as a foundation:
var processInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
processInfo.workingDirectory = file.parent;
processInfo.executable = new File("C:\\WINDOWS\\system32\\cmd.exe");
var args:Vector.<String> = new Vector.<String>();
args.push("/c");
args.push(file.name);
processInfo.arguments = args;
var process:NativeProcess = new NativeProcess();
try
{
process.start(processInfo);
}
catch(e:Error)
{
//give up - open the folder
file.parent.openWithDefaultApplication();
}
The "Open with..." dialog on Windows shows whenever you try to launch an application that does not have an associate file type. Launch the application with NativeProcess.start() and Windows will take care of either opening it with the correct program, or presenting a selection screen if there is no file type associated.
Alternatively, depending on what you are trying to do, you may be interested in associating file types with your AIR application. For instance, you can associate PNG files with your AIR app so that they will by default open in your AIR app (e.g. double-clicking a PNG file will launch your app with an INVOKE event specifying the PNG file that was opened). See http://livedocs.adobe.com/flex/3/html/help.html?content=File_formats_1.html under "Declaring file type associations" for more information.
Of course, your AIR app may have no capabilities to handle opening whatever file you are trying to open and you want a different program to handle it, in which case you can just use the first method which will let the operating system take care of launching the application with the correct program.
EDIT :
I got confused as to what Native Process actually does. NativeProcess launches executables - NOT files, so the method above won't work.
Instead of trying to open the file directly with NativeProcess, try opening the program that you want to open the file with and pass the file in as an argument. For instance, if you want to open a PNG file with some special image program, you would do something like this:
var imageEditorProgram:File = new File("C:/Path/To/Program.exe");
var args:Vector.<String> = new Vector.<String>();
args.push("C:/Path/To/Image.png");
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = imageEditorProgram;
nativeProcessStartupInfo.arguments = args;
var nativeProcess:NativeProcess = new NativeProcess();
nativeProcess.start(nativeProcessStartupInfo);
This should open the image editing program with the image file passed in.
However, the downside with this method is that you can't just let the operating system determine which program to open the file with. You should be able to open a file browse dialog and allow the user to select the program to open the file with (similar to how the operating system does it). So, hopefully the following would do what you want:
var file:File = File.userDirectory;
file.addEventListener(Event.SELECT, selectHandler);
file.browse();
private function selectHandler(e:Event):void{
file.removeEventListener(Event.SELECT, selectHandler);
var filePath:String= file.nativePath;
var imageEditorProgram:File = new File(filePath);
var args:Vector.<String> = new Vector.<String>();
args.push("C:/Path/To/Image.png");
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = imageEditorProgram;
nativeProcessStartupInfo.arguments = args;
var nativeProcess:NativeProcess = new NativeProcess();
nativeProcess.start(nativeProcessStartupInfo);
}
I haven't tested the above code, but it should open a file browse dialog allowing the user to select a program to open the file with (perhaps you should add a FileFilter to restrict the user to selecting only .exe), and then launch that program using NativeProcess.
For user-friendliness you should consider saving user preferences for filetypes (so instead of forcing them to go through the file browser every time they want to open a PNG, just remember that they like to open PNG's with AwesomeImageEditor.exe).