Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] How do i upload a photo from the Android camera with FormData on Ionic / Angular

Posted on November 11, 2022 By

Solution 1 :

Problem is destinationType: this.camera.DestinationType.FILE_URI,

you are sending, file url over http and not the base64 of image

Change your destination type:
destinationType: this.camera.DestinationType.DATA_URL,

DATA_URL Return base64 encoded string. DATA_URL can be very memory intensive and cause app crashes or out of memory errors. Use FILE_URI or NATIVE_URI if possible

UPDATE

In this video you can check how to send base64 to api as File

https://www.youtube.com/watch?v=tph5Nk4Ab1g

Problem :

I’m currently developing a Android app where the user can take his or hers picture and upload it to a PATCH API endpoint that would listen to the key ‘avatar’.

I’m using the Cordova Camera and the Advanced HTTP plugin to handle it.

Below is the function that triggers when taking a photo.

takePicture() {
    const options: CameraOptions = {
      quality: 50,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      correctOrientation: true,  // Corrects Android orientation quirks
      allowEdit: false, // Post process aanpassingen
      sourceType: this.camera.PictureSourceType.CAMERA // Pak de selfie camera
    };
    this.camera.getPicture(options).then((imageData) => {
      const formData = new FormData();
      formData.append('avatar', imageData, 'pic.jpg');
      this.web.updateUserInfo(formData).subscribe(() => {});
    }, (err) => {
      console.error('Camera Error: ' + err);
    });
  }

Here is the API handling

updateUserInfo(newData: any) {
        return new Observable((obs) => {
                this.http2.patch('localhost/user', {newData}, {
                    'X-Subdomain': 'host',
                    'X-Token': this.apiKey,
                }).then(() => {console.log('Camera API success!'); obs.next(); }).catch(error => {
                    console.error(error);
                });
        });
    }

No errors are being given out so it is hard for me to see where the issue is. I have little experience working with Cordova and Ionic so this is all new to me.

Comments

Comment posted by parrycima

Application side everything is working fine then. Cordova camera gives you the base64 of image. You have to debug on you server side or where you want to show your image.

READ  [FIXED] c# - How to update values and selected index in Alert Dialog builder setSingleChoiceItems
Powered by Inline Related Posts

Comment posted by Sjoerd

Do I need to put this Base64 string in formData.append as well like how it currently goes? When checking Swagger for the API details, the API requires Parameter Type: FormData with Data Type: file.

Android Tags:android, angular, ionic-framework, multipartform-data

Post navigation

Previous Post: [FIXED] Xamarin.forms – Trouble behaviour between ListView & keyboard
Next Post: [FIXED] android – two elements in linear layout are packed to center instead of to the sides

Related Posts

[FIXED] android – How to change the radius of radio button Android
[FIXED] Can I use python’s pyglet library on android? Android
[FIXED] google play – Android TV application is not passing review Android
[FIXED] android – DatePickerDialog resets to default month when changing selectableDays Android
[FIXED] android – How to make Textinputlayout hint multi-line? Android
[FIXED] android – How is better to inject a ViewModel object when using Jetpack Compose? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme