Answered by:
Share source from a local file

Question
-
Hi,
I would like my app to be a share source, and there is one issue about my source file.
sdkSample.currentPSPDoc.workingImagePath is a absolute path to a jpeg file in "TempState" folder.
In this kind of codes, it always shows a message like "XXXX can not share".
However, if I do not create StorageFile from path, but to use StorageFile from OpenFilePicker, it does work. The question is, is it the right way to return the bitmap/storageitem? Thanks.
Alex
Windows.Storage.StorageFile.getFileFromPathAsync(sdkSample.currentPSPDoc.workingImagePath).then( function (newDocWorkFile) { // create stream from path request.data.properties.title = newDocWorkFile.name; // Title required request.data.properties.description = newDocWorkFile.path; // Description optional // When sharing an image, don't forget to set the thumbnail for the DataPackage var streamReference = Windows.Storage.Streams.RandomAccessStreamReference.createFromFile(newDocWorkFile); request.data.properties.thumbnail = streamReference; // It's recommended to always use both setBitmap and setStorageItems for sharing a single image // since the Target app may only support one or the other // Put the image file in an array and pass it to setStorageItems request.data.setStorageItems([newDocWorkFile]); // The setBitmap method requires a RandomAccessStreamReference request.data.setBitmap(streamReference); } );
Tuesday, March 27, 2012 2:53 AM
Answers
-
I believe the problem is the absolute path, try usign the known folders instead.
-Jeff
Jeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, March 27, 2012 2:51 PM
- Marked as answer by Jie Bao Tuesday, April 10, 2012 3:47 AM
Tuesday, March 27, 2012 2:51 PMModerator -
Thanks for reply. I tried to use a photo in my "LocalState" folder, still the same. use URI, the same.
Then, I tried to move the FileStorage object out of the event handler, then it can work.
So my codes are now like attached. That works for me, just need to keep a FileStorage object and not sure if that takes lots of resource or not.
Alex
// some where in my code, I need to generate sdkSample.ShareFileObj, and this is still the photo from an absolute path function fnPrepareDocShareSource() { sdkSample.ShareFileObj = null; { if (sdkSample.currentPSPDoc != null) { Windows.Storage.StorageFile.getFileFromPathAsync(sdkSample.currentPSPDoc.workingImagePath).then( function (newDocWorkFile) { sdkSample.ShareFileObj = newDocWorkFile; }); } } } //then, in the event handler, use that object can work. function imageDataRequestedHandler(request) { if (sdkSample.ShareFileObj) { // create stream from path request.data.properties.title = "Share Image"; // Title required request.data.properties.description = sdkSample.ShareFileObj.name; // Description optional // When sharing an image, don't forget to set the thumbnail for the DataPackage var streamReference = Windows.Storage.Streams.RandomAccessStreamReference.createFromFile(sdkSample.ShareFileObj)//newDocWorkFile); request.data.properties.thumbnail = streamReference; // It's recommended to always use both setBitmap and setStorageItems for sharing a single image // since the Target app may only support one or the other // Put the image file in an array and pass it to setStorageItems request.data.setStorageItems([sdkSample.ShareFileObj]);//newDocWorkFile]); // The setBitmap method requires a RandomAccessStreamReference request.data.setBitmap(streamReference); } }
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, March 28, 2012 2:39 PM
- Marked as answer by Jie Bao Tuesday, April 10, 2012 3:47 AM
Wednesday, March 28, 2012 7:25 AM
All replies
-
I believe the problem is the absolute path, try usign the known folders instead.
-Jeff
Jeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, March 27, 2012 2:51 PM
- Marked as answer by Jie Bao Tuesday, April 10, 2012 3:47 AM
Tuesday, March 27, 2012 2:51 PMModerator -
You can also use getFileFromApplicationUriAsync() and pass in something like "ms-appx:///packageFile.txt" or "ms-appdata:///appDataFile.txt" to obtain storage files for things in the install directory or local application data folder. The latter approach would require you to store the photo in the local app data folder instead of the temp app data folder.
Justin Cooperman
Tuesday, March 27, 2012 8:25 PM -
Thanks for reply. I tried to use a photo in my "LocalState" folder, still the same. use URI, the same.
Then, I tried to move the FileStorage object out of the event handler, then it can work.
So my codes are now like attached. That works for me, just need to keep a FileStorage object and not sure if that takes lots of resource or not.
Alex
// some where in my code, I need to generate sdkSample.ShareFileObj, and this is still the photo from an absolute path function fnPrepareDocShareSource() { sdkSample.ShareFileObj = null; { if (sdkSample.currentPSPDoc != null) { Windows.Storage.StorageFile.getFileFromPathAsync(sdkSample.currentPSPDoc.workingImagePath).then( function (newDocWorkFile) { sdkSample.ShareFileObj = newDocWorkFile; }); } } } //then, in the event handler, use that object can work. function imageDataRequestedHandler(request) { if (sdkSample.ShareFileObj) { // create stream from path request.data.properties.title = "Share Image"; // Title required request.data.properties.description = sdkSample.ShareFileObj.name; // Description optional // When sharing an image, don't forget to set the thumbnail for the DataPackage var streamReference = Windows.Storage.Streams.RandomAccessStreamReference.createFromFile(sdkSample.ShareFileObj)//newDocWorkFile); request.data.properties.thumbnail = streamReference; // It's recommended to always use both setBitmap and setStorageItems for sharing a single image // since the Target app may only support one or the other // Put the image file in an array and pass it to setStorageItems request.data.setStorageItems([sdkSample.ShareFileObj]);//newDocWorkFile]); // The setBitmap method requires a RandomAccessStreamReference request.data.setBitmap(streamReference); } }
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, March 28, 2012 2:39 PM
- Marked as answer by Jie Bao Tuesday, April 10, 2012 3:47 AM
Wednesday, March 28, 2012 7:25 AM -
Hi,
can any one provide me sample project of above .
It is not working on my machine
Thanks,
Manoj
kumarmanoj@fareportal.com
Tuesday, June 19, 2012 11:54 AM