我们可以从手机(Android/iOS)中选取图片,然后使用通用代码(Xamarin Forms)将其上传到服务器吗? [英] Can we pick a Image from Phone (Android / iOS) and upload it to server with common code(Xamarin Forms)?

查看:202
本文介绍了我们可以从手机(Android/iOS)中选取图片,然后使用通用代码(Xamarin Forms)将其上传到服务器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要实现这一目标,我应该使用平台特定的代码,还是可以从Xamarin.Forms共享项目中的通用代码中完成.

to achieve this should I go for platform specific code or Can I do that from common code in shared Project of Xamarin.Forms.

基本上,我需要打开Gallery并选择一个图像,然后将其以多部分字节数组的形式上传到服务器.

Basically I need to open Gallery and pick a image and then upload it to server in multipart byte array.

使用Xamarin表单找不到任何示例.

Couldn't found any examples using Xamarin Forms.

推荐答案

我假设您正在使用PCL解决方案.

I am assuming you are using a PCL solution.

有很多方法可以做到这一点. 此处是我用来允许用户选择图像的插件从图库中获取图片,然后对该图像进行处理.

There are quite a few ways to do this. Here is the plugin that I used to allow the user to pick an image from the gallery and then do something with that image.

尽管您也可以使用XLabs版本,但可以在此处

Though you could also use XLabs version as well, available here.

使用我建议的插件,将其安装到PCL项目,Android项目和iOS项目中之后,您可以编写如下代码来显示图像选择器,然后对该图像执行某些操作:

With the plugin I suggested, after installing it into the PCL project, the Android project, and the iOS project, you could write code like this to show an image picker and then do something with that image:

public List<string> Uris;

private ObservableCollection<MediaFile> _images;
public  ObservableCollection<MediaFile> Images {
    get { return _images ?? (_images = new ObservableCollection< MediaFile >()); }
    set {
        if(_images != value) {
            _images = value;
            OnPropertyChanged();
        }
    }
}

/// <summary>
/// Allows the user to pick a photo on their device using the native photo handlers and returns a stream, which we save to disk.
/// </summary>
/// <returns>string, the name of the image if everything went ok, 'false' if an exception was generated, and 'notfalse' if they simply canceled.</returns>
public async Task<string> PickPictureAsync() {

    MediaFile file      = null;
    string    filePath  = string.Empty;
    string    imageName = string.Empty;

    try {
        file = await CrossMedia.Current.PickPhotoAsync();

        #region Null Check

         if(file == null) { return null; }                                                                                 //Not returning false here so we do not show an error if they simply hit cancel from the device's image picker

        #endregion

        imageName = "SomeImageName.jpg";
        filePath  = /* Add your own logic here for where to save the file */ //_fileHelper.CopyFile(file.Path, imageName);
    } catch(Exception ex) {
         Debug.WriteLine("\nIn PictureViewModel.PickPictureAsync() - Exception:\n{0}\n", ex);                           //TODO: Do something more useful
         return null;
    } finally { if(file != null) { file.Dispose(); } }

    Receipts.Add(ImageSource.FromFile(filePath));

    Uris.Add(filePath);

    return imageName;
}

这篇关于我们可以从手机(Android/iOS)中选取图片,然后使用通用代码(Xamarin Forms)将其上传到服务器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆