2014-07-13 11 views
8

मैं विंडोज़ फोन 8.1 में अपने चित्र एल्बम से एक छवि चुनना चाहता हूं। इस के लिए मैं इस कोड का इस्तेमाल किया है, लेकिन इसके त्रुटिविंडोज फोन में फ़ाइल पिकर 8.1

private async void gallery_Tapped(object sender, TappedRoutedEventArgs e) 
     { 
      FileOpenPicker opener = new FileOpenPicker(); 
      opener.ViewMode = PickerViewMode.Thumbnail; 
      opener.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
      opener.FileTypeFilter.Add(".jpg"); 
      opener.FileTypeFilter.Add(".jpeg"); 
      opener.FileTypeFilter.Add(".png"); 

      StorageFile file = await opener.PickSingleFileAsync(); 
      if (file != null) 
      { 
       // We've now got the file. Do something with it. 
       var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); 
       var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); 
       await bitmapImage.SetSourceAsync(stream); 

       var decoder = await    Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream); 
       MyImage.Source=bitmapImage; 
      } 
      else 
      { 
       //OutputTextBlock.Text = "The operation may have been cancelled."; 
      } 
     } 

त्रुटि

enter image description here

+1

आप [PickSingleFileAndContinue] उपयोग नहीं करना चाहिए (http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.pickers.fileopenpicker.picksinglefileandcontinue। aspx?)? –

+1

Ulugbek की तरह कहा था - आप विंडोज फोन को लक्षित कर रहे हैं और आप उन विधियों (* PickSingleFileAsync() *) का उपयोग नहीं कर सकते हैं, इसलिए फ़ाइल को चुनते समय आपका ऐप समाप्त हो सकता है। आपको उपर्युक्त विधि का उपयोग करना होगा - अधिक संदर्भ और एक अच्छा उदाहरण आपको मिलेगा [यहां एमएसडीएन पर] (http://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn614994.aspx)। – Romasz

+0

हां मैं इसे सही करता हूं लेकिन उस चयनित फ़ाइल के साथ कैसे काम करना है जो शून्य हो जाता है? –

उत्तर

15

देता है मुझे लगता है कि आप जिस पृष्ठ पर जहां आवश्यक में भी OnActivated घटना संभाल कर सकते हैं। कुछ इस तरह

CoreApplicationView view = CoreApplication.GetCurrentView(); 

ImagePath=string.Empty; 
FileOpenPicker filePicker = new FileOpenPicker(); 
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
filePicker.ViewMode = PickerViewMode.Thumbnail; 

// Filter to include a sample subset of file types 
filePicker.FileTypeFilter.Clear(); 
filePicker.FileTypeFilter.Add(".bmp"); 
filePicker.FileTypeFilter.Add(".png"); 
filePicker.FileTypeFilter.Add(".jpeg"); 
filePicker.FileTypeFilter.Add(".jpg"); 

filePicker.PickSingleFileAndContinue(); 
view.Activated += viewActivated; 

private void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1) 
{ 
    FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs; 

    if (args != null) 
    { 
     if (args.Files.Count == 0) return; 

     view.Activated -= viewActivated; 
     storageFileWP = args.Files[0]; 

    } 
} 

जब आप पिकर से फ़ाइलों का चयन करते हैं तो उपरोक्त विधि को कॉल किया जाएगा। मेरा मानना ​​है कि यह आपकी मदद करता है।

3

उपयोग RoutedEventArgs बजाय WP 8.1 XAML में बटन क्लिक के लिए TappedRoutedEventArgs का प्रयोग न करें async कुंजी शब्द

private void OpenImageFile(object sender, RoutedEventArgs e) 
 
{    
 
      FileOpenPicker filePicker = new FileOpenPicker(); 
 
      filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
 
      filePicker.ViewMode = PickerViewMode.Thumbnail; 
 

 
      // Filter to include a sample subset of file types 
 
      filePicker.FileTypeFilter.Clear(); 
 
      filePicker.FileTypeFilter.Add(".bmp"); 
 
      filePicker.FileTypeFilter.Add(".png"); 
 
      filePicker.FileTypeFilter.Add(".jpeg"); 
 
      filePicker.FileTypeFilter.Add(".jpg"); 
 

 
      filePicker.PickSingleFileAndContinue(); 
 
      view.Activated += viewActivated; 
 
} 
 

 
private void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1) 
 
{ 
 
      FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs; 
 

 
      if (args != null) 
 
      { 
 
       if (args.Files.Count == 0) return; 
 

 
       view.Activated -= viewActivated; 
 
       StorageFile SelectedImageFile = args.Files[0]; 
 

 
      } 
 
}

  • और CoreApplicationView दृश्य का उपयोग; ग्लोबल
  • के रूप में हर विधि के वर्ग के बाहर की ओर कहीं भी देखें = CoreApplication.GetCurrentView(); InitializeComponent() के बाद प्रासंगिक पृष्ठ वर्ग के निर्माता के अंदर; विधि

मुझे लगता है कि इस में मदद मिलेगी :) धन्यवाद

0

वर भरने = इंतजार StorageFile.GetFileFromPathAsync (selectItem.FolderPath); बिटमैप छवि बिट = नया बिटमैप छवि();

    if (fill != null) 
        { 
         // We've now got the file. Do something with it. 
         var stream = await fill.OpenAsync(Windows.Storage.FileAccessMode.Read); 
         //var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); 
         //await bitmapImage.SetSourceAsync(stream); 
         bit.SetSource(stream); 
         imgTeste.Source = bit; 
         pvMestre.SelectedIndex = 1; 
        } 
        else 
        { 
         //OutputTextBlock.Text = "The operation may have been cancelled."; 
        } 
+0

क्या आप अपने कोड में स्पष्टीकरण जोड़ सकते हैं? –

12

चित्र गैलरी से चित्र चुनने के लिए विंडोज फोन 8.1 में FileOpenPicker का उपयोग करना।

चरण 1: अपने विंडोज फोन 8.1 ऐप में चित्र लाइब्रेरी क्षमता जोड़ें।

Picture Library Capability

चरण 2: फ़ाइल खोलें पिकर एक घोषणा के रूप में।

File Open Picker declaration

चरण 3: MainPage.xaml के लिए एक बटन और छवि जोड़ें।

<Grid> 
<Image Name="img"/> 
<Button Content="click me" Click="Button_Click"/> 
</Grid> 

चरण 4: वैश्विक चर दृश्य जोड़ें।

CoreApplicationView view; 

चरण 4.1 पृष्ठ निर्माता में आरंभ करें।

view = CoreApplication.GetCurrentView(); 

चरण 5: बटन क्लिक ईवेंट पर फ़ाइल ओपन पिकर को कॉल करने के लिए कोड जोड़ें।

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    FileOpenPicker filePicker = new FileOpenPicker(); 
    filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
    filePicker.ViewMode = PickerViewMode.Thumbnail; 

    // Filter to include a sample subset of file types 
    filePicker.FileTypeFilter.Clear(); 
    filePicker.FileTypeFilter.Add(".bmp"); 
    filePicker.FileTypeFilter.Add(".png"); 
    filePicker.FileTypeFilter.Add(".jpeg"); 
    filePicker.FileTypeFilter.Add(".jpg"); 

    filePicker.PickSingleFileAndContinue(); 
    view.Activated += viewActivated; 
} 

चरण 6: सक्रिय ईवेंट देखें छवि को मुख्य पृष्ठ पर सेट करें।

private async void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1) 
{ 
    FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs; 

    if (args != null) 
    { 
     if (args.Files.Count == 0) return; 

     view.Activated -= viewActivated; 
     StorageFile storageFile = args.Files[0]; 
     var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read); 
     var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); 
     await bitmapImage.SetSourceAsync(stream); 

     var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream); 
     img.Source=bitmapImage; 
    } 
} 

यह आपको एक फोटो लेने और इसका उपयोग करने की अनुमति देता है।

संदर्भ: Using FileOpenPicker in Windows Phone 8.1 to choose picture from Picture Gallery

+0

आपने कभी भी अपने कोड के अंदर ImagePath चर का उपयोग नहीं किया है। ये किसके लिये है? –

+0

आप सही हैं! मैंने कभी इसका इस्तेमाल नहीं किया, मुझे यह भी नहीं पता कि यह क्यों है, मैं अपने वर्तमान कार्यान्वयन को देख रहा हूं और ImagePath वहां नहीं है। मैं अभी अपना जवाब संपादित कर रहा हूं। धन्यवाद! – isscroberto

+0

क्या गिनती की जांच से पहले घटना हैंडलर को हटाया जाना चाहिए, यदि उपयोगकर्ता ने फ़ाइल पिकर को कोई चयन नहीं छोड़ा है? – dlatikay

संबंधित मुद्दे