Android Aws Upload Bitmap Object to S3

This post has been deprecated. Instead, please meet the new Dilate Android Getting Started tutorial to learn how to build Android mobile applications using AWS Amplify.

This is function two of a two-function series of walkthroughs on how to build an AWS cloud-enabled Android mobile app with the AWS Amplify toolchain.

In this post, we proceed from part 1 and add more avant-garde features to our Android app. Nosotros encompass the post-obit:

  • Using optimistic updates: AWS AppSync API offline support
  • Using subscriptions on data changes (mutations)
  • Enabling object storage through Amazon S3

Prerequisites

To create an Android project, you need to take the Java JDK installed on your work station. Download and install Android Studio, and and then download the Android 6.0 SDK (API level 23 or higher) in the Android SDK Manager.

Also download an emulator prototype. To practice so, choose AVD Manager in Android Studio. Choose + Create Virtual Device, and then follow the instructions to complete setup.

Continuing from office one

At the end of the last postal service, we had created an Android app that displays a listing of pets, and lets you add new pets. After adding a new pet, the app looks similar this:

Optimistic updates and offline support

With the optimistic update functionality, you tin can provide a more responsive end user experience. Y'all configure the UI so that it behaves as if the server will eventually return the data we look. It's beingness optimistic that the update is successful.

In this section, we create the data that we expect to be returned in retentivity after the mutation, and write it to the persistent SQL store that the Android device manages. And then when the server update returns, the SDK consolidates the data for you.

This approach works well with the scenario where internet connectivity is cut off while you're trying to modify data. The AWS AppSync SDK automatically reconnects and sends the mutation when the app goes online.

Now let'southward endeavor it out. Open AddPetActivity.coffee, and add the offline support at the end of relieve():

At present permit's add the addPetOffline method. We bank check for connectivity afterward writing to the local cache, and shut the activeness every bit if the addition was successful.

We don't need to modify MainActivity because its query() method uses the CACHE_AND_NETWORK approach. It reads from the local cache first while making a network call. Our previously added pet already exists in the local cache considering of optimistic update.

Build and run the app. Later y'all sign in, plough on Plane mode:

Go back to the app and try adding a new item. The UI experience should exist the same as when you lot were online earlier. Enter a name and clarification:

Choose Salvage. The app should display the second particular in the listing.

Now turn Airplane mode off. The items should be automatically saved. You tin can verify that the salve is successful by closing and reopening the app, then confirming that you still see the aforementioned two items beingness displayed.

Subscriptions

With AWS AppSync, you can use subscriptions for real-time notifications.

In this section, we use a subscription to permit us know correct away when someone else adds a new pet. To do this, allow's add the following block at the end of the MainActivity.java class:

Then allow's change the onResume method to telephone call subscribe at the end, to subscribe to new pet creations. We likewise need to brand sure that we unsubscribe when we're washed with the activity.

Now allow'southward exam it out. Build and run our app on your emulator.

Let'southward then commencement up a second emulator. To get-go a second emulator, make certain that you take the default unselected. (Choose Run, choose Edit configurations. NetherAndroid App, chooseapp, and clear theUse same device for future launches cheque box).

Also make sure that y'all take a different emulator device blazon in the AVD Manager. Run the app, cull your second emulator device, and have the app running side by side in these two emulators. Make certain that y'all sign into both so that y'all're looking at the list of pets on both devices.

Add another pet in one of the apps, and scout information technology appear in the other app. Viola!

Working with storage

With AWS Amplify, yous tin easily add together object storage support by using Amazon S3. AWS Dilate manages the bucket provision and permission configuration for you automatically.

Update AWS Amplify

To get started, let's modify the local schema in ./dilate/backend/api/<PROJECTNAME>/schema.graphql to add a photograph string holding:

Side by side, go to our root directory, and run the following at the command line:

Respond the following questions:

  • Please select from one of the below mentioned services:Content (Images, sound, video, etc.)
  • Delight provide a friendly name for your resource that will be used to label this category in the project:MyPetAppResources
  • Please provide bucket name:mypetapp1246e0cde8074f78b94363dbe73f8adfdsfds (or something unique)
  • Who should have access:Auth users merely
  • What kind of access practise you desire for Authenticated users:read/write

So run:

CullY when you're asked whether you desire to update lawmaking and regenerate GraphQL statements. ChooseEnter, and wait for AWS CloudFormation updates to cease. This takes a couple of minutes.

Add storage dependencies

Meanwhile, let's update our front-end customer code.

Open AndroidManifest.xml, and add together the TransferService in our <application>:

Open up your app's build.gradle, and add the dependency for Amazon S3:

Add photo option code

Next, open up AddPetActivity.java, and add the photo selection code:

We demand to telephone call the upload photo from the UI. Open activity_add_pet.xml, and add a push button before the Salvage push:

Now let's connect this button to our choosePhoto() method. Go back to AddPetActivity.java, and alter onCreate to adhere a click listener to the Salve button:

Build and run the app to confirm that the photo selection button works:

Choose Add Photo. You should be able to select a photo from your gallery. (If there's no photo in the emulator, open the browser and download some photos from the internet.)

After y'all select a photo, you should be redirected back to the Add screen.

Add Amazon S3 photograph uploading code

Now that the photo tin can be selected, we need to make sure that the photograph gets uploaded and stored in the backend. Nosotros'll use TransferUtility to handle the Amazon S3 file upload and download. Permit'south add together its initialization code to our ClientFactory.coffee grade.

Adjacent, allow's add lawmaking to upload the photograph by using the TransferUtility in our AddPetActivity.coffee:

Save photo in mutation

Because nosotros've added a new holding to the Pet object, we need to modify our code to accommodate it. In AddPetActivity.java, excerpt the following method to produce different types of CreatePetInputs, depending on whether a photograph has been selected:

Next, nosotros alter our salve() to phone call the extracted method:

Considering we changed the CreatePet mutation, nosotros need to modify the addPetOffline lawmaking every bit well:

So we tin create a new method, uploadAndSave(), to handle both the photo and no-photo saves:

At present we can phone call our uploadAndSave() functions when the Save push button is chosen:

We also take to update the subscription callback in MainActivity.java because of the newly added photo property:

Download and display photos

At present that nosotros've implemented the ability to salvage photos, permit'due south make certain that they become downloaded and displayed.

Open recyclerview_row.xml, add an <ImageView>, and modify the layout every bit follows:

Open up MyAdapter.java. Add together code to correspond to the photo property, and to download the photograph if one exists.

Because we're downloading photos, we need to make certain that permissions can be granted. Go to MainActivity.java, and add the permission-seeking block in query():

Now we're finally done! Build and run the app once again. Check to see if you tin add a photo and see your lovely pet! Your app should expect something like this:

Other features

At that place are other enhancements that nosotros can brand to the app. Try to work on the following as practice for yourself:

  • Add the capability to update a pet's information.
  • Add the ability to delete a pet.
  • Subscribe to update and delete mutations.

Requite us feedback

We're excited that you lot've created this Android app and have added more advanced features to it. As always, let us know how we're doing, and submit any requests in the AWS Dilate CLI repository. You tin read more than near AWS Amplify on the new AWS Amplify website.

Jane Shen is an AWS Professional person Services Awarding Architect based in Toronto, Canada.

murtaghablencesee.blogspot.com

Source: https://aws.amazon.com/blogs/mobile/building-an-android-app-with-aws-amplify-part-2/

0 Response to "Android Aws Upload Bitmap Object to S3"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel