Question

Hello I want to get the path from a file in the isolatedStora

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream targetStream = isoStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
    {  
        // Initialize the buffer for 4KB disk pages.
        byte[] readBuffer = new byte[4096];
        int bytesRead = -1;

        // Copy the thumbnail to isolated storage. 
        while ((bytesRead = e.Result.Read(readBuffer, 0, readBuffer.Length)) > 0)
        {
            targetStream.Write(readBuffer, 0, bytesRead);
        }  
        targetStream.Close();

        _myObject.object_image = targetStream.Name ;
    }
}

This works perfectly with windows Phone 8 but not with Widows phone 7. On Windows Phone 7 there an exception is thrown

{System.MethodAccessException: Attempt to access the method failed: System.IO.IsolatedStorage.IsolatedStorageFileStream.get_Name()
    at deepView.Model.HistoryHelper.<saveObjectToHistory>b__1(Object s, OpenReadCompletedEventArgs e)
    at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
    at System.Net.WebClient.OpenReadOperationCompleted(Object arg)
    at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
    at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
    at System.Delegate.DynamicInvokeOne(Object[] args)
    at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
    at System.Delegate.DynamicInvoke(Object[] args)
    at System.Windows.Threading.DispatcherOperation.Invoke()
    at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
    at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
    at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
    at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
    at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
}

Can anybody help me please ?

EDIT: complete Code where i get the image and save it to the isolated Storage (which works on WP8 perfectly) and the highlighted part is where i try to get the name and save it to db

WebClient client = new WebClient();
        client.OpenReadCompleted += (s, e) =>
        {
            try
            {
                string fileName = "Shared/Media/object_image_6200.jpg";
                //Save thumbnail as JPEG to isolated storage.
                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!isoStore.FileExists(fileName))
                    {
                        using (IsolatedStorageFileStream targetStream = isoStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                        {                              
                            // Initialize the buffer for 4KB disk pages.
                            byte[] readBuffer = new byte[4096];
                            int bytesRead = -1;

                            // Copy the thumbnail to isolated storage. 
                            while ((bytesRead = e.Result.Read(readBuffer, 0, readBuffer.Length)) > 0)
                            {
                                targetStream.Write(readBuffer, 0, bytesRead);
                            }                              


                            **_historyObject.object_image = targetStream.Name;**
                            targetStream.Close();
                            HistoryClass existingHistoryObject = null;
                            existingHistoryObject = db.FindObjectByObjectId(GlobalVariables.responseObject.object_id);
                            if (existingHistoryObject == null)
                            {
                                db.HistoryObjects.InsertOnSubmit(_historyObject);
                                db.SubmitChanges();
                            }
                        }                            
                    }
                }                    
            }
            catch (Exception ex)
            {
                //EXCEPTION HANDLING
            }

        };
        //get object image for history view
        client.OpenReadAsync(new Uri("http://example.de/object_6200.jpg", UriKind.Absolute));

......

But the exception is thrown when the execution arrives the target.Name

Was it helpful?

Solution

Regardless of the cause of the issue, I couldn't reproduce it, you don't need the full path to display your images in the UI. You can use a relative path instead.

edit:

Ok, to make this work try the following:

Add a new property to your _historyObject of type BitmapImage:

  BitmapImage MyBitmap { get; set; }

When you load your data from the database in GetHistoryObjects(), load the Bitmap field from the stored file path:

  MyBitmap = GetBitmap(object_image);

  private BitmapImage GetBitmap(string path) {
    var bi = new BitmapImage();
    using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) {
      using (var fileStream = myIsolatedStorage.OpenFile(path, FileMode.Open, FileAccess.Read)) {
        bi.SetSource(fileStream);
      }
    }
    return bi
  }

Finally, bind the image control source property to MyBitmap instead of object_image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top