UWP browse the picture and save in assets

I have create the UWP which include sign up step and in sign up step I would like user to upload the pictures and I wish when user upload it's will save in the assets folder automatically I would like to know 2 things

  1. How do I can access the picture (May be from camera roll or take a picture)

  2. How do I can save that picture user upload to asset folder

Thank you

Upvotes: 0

Views: 1277

Answers (1)

Damir Arh
Damir Arh

Reputation: 17855

Getting the picture

To let the user pick it from his pictures library:

var picker = new FileOpenPicker
{
    SuggestedStartLocation = PickerLocationId.PicturesLibrary,
    FileTypeFilter = {".jpg", ".jpeg", ".png", ".gif"}
};
var file = await picker.PickSingleFileAsync();

To take a photo:

var capture = new CameraCaptureUI
{
    PhotoSettings =
    {
        Format = CameraCaptureUIPhotoFormat.Jpeg
    }
};
var file = await capture.CaptureFileAsync(CameraCaptureUIMode.Photo);

In both cases the result is an IStorageFile.

Saving the picture

I'm not really sure, what you mean by "asset folder". The Assets folder in your project is a part of the application and only contains the files that were distributed with the original application package. You can't save additional files there.

If you want to save the files locally, you can use one of the application storage folders, e.g. LocalFolder:

await file.CopyAsync(ApplicationData.Current.LocalFolder, "profile.jpg");

If you want to upload it to a server and have it accessible globally to all users, you'll of course need some server side code for that, e.g. an upload page to post the image to:

var uri = new Uri("http://my.server.com/upload");
var uploader = new BackgroundUploader();
var upload = uploader.CreateUpload(uri, file);
await upload.StartAsync();

You could then save the file somewhere on the server and make it accessible to the apps. Check out the official sample to see a working implementation of file upload.

Upvotes: 1

Related Questions