Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Tuesday, 6 March 2018

Android image cropping from Camera / Gallery

Recently I needed to implement avatar image upload from an Android app, I didn't found any example that did all that I needed so i make this tutorial .

-> Requirements:

  • Using single chooser pickup image from camera or gallery .
  • Select user picture and you can use cropping functionality for your avatar.
  • Avatar image size fixed 500×500 pixels.
  • Usage of memory is efficient.

-> Obstacles:

  • Creating single chooser intent for camera and gallery is not so trivial.
  • Android crop activity is limited, unreliable and is internal API.
  • May be image will be rotated after selecting from the camera or gallery
  • If picked image is large so sometimes it creates issues of memory leak.
-> Powerful (Zoom, Rotation, Multi-Source), customizable (Shape, Limits, Style), optimized (Async, Sampling, Matrix) and simple image cropping library for Android.
  1. Include the library
compile 'com.theartofdev.edmodo:android-image-cropper:2.6.+' 
-> Add permissions to manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Using Activity

  1. Add CropImageActivity into your AndroidManifest.xml
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
  android:theme="@style/Base.Theme.AppCompat"/> 
<!-- optional (needed if default theme has no action bar) -->
  1. Start CropImageActivity using builder pattern from your activity
// start picker to get image for cropping and then use the image in cropping activity
CropImage.activity()
  .setGuidelines(CropImageView.Guidelines.ON)
  .start(this);

// start cropping activity for pre-acquired image saved on the device
CropImage.activity(imageUri)
 .start(this);

// for fragment (DO NOT use `getActivity()`)
CropImage.activity()
  .start(getContext(), this);
  1. Override onActivityResult method in your activity to get crop result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
    CropImage.ActivityResult result = CropImage.getActivityResult(data);
    if (resultCode == RESULT_OK) {
      Uri resultUri = result.getUri();
    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
      Exception error = result.getError();
    }
  }
}

Using View

  1. Add CropImageView into your activity
<!-- Image Cropper fill the remaining available height -->
<com.theartofdev.edmodo.cropper.CropImageView
  xmlns:custom="http://schemas.android.com/apk/res-auto"
  android:id="@+id/cropImageView"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"/>
  1. Set image to crop
cropImageView.setImageUriAsync(uri);
// or (prefer using uri for performance and better user experience)
cropImageView.setImageBitmap(bitmap);
  1. Get cropped image
// subscribe to async event using cropImageView.setOnCropImageCompleteListener(listener)
cropImageView.getCroppedImageAsync();
// or
Bitmap cropped = cropImageView.getCroppedImage();
-> Referecne :  https://github.com/ArthurHub/Android-Image-Cropper
Thank you!!





































Monday, 21 November 2016

Android Development Tutorial From Starting Point

Hello friends , Don’t worry if you don’t even know the ‘A’ of Android App Development. I will try to tell you everything about Android Application Development. So this is the very first post of this series so in this post I will tell you about configuring your system for android application development tutorial.


What you need for this Android Application Development Tutorial Series

  • A Computer (Whether it is MAC , Windows , Ubuntu , etc)

What you should already know before starting?

  • JAVA Programming Language. You should know the basics concepts of JAVA.

Software Requirements

  • Java Development Kit (JDK)
  • Android Studio with SDK

Installing Java Development Kit

  • First we need to install JDK. So get the JDK from this link.
  • In my case I have downloaded jdk8 64bit for windows, because I am using Windows Operating System
  • Install JDK (next -> next -> next -> finish as we always do :P)
  • Now we need to set an environment variable

Setting JAVA_HOME variable

  • Right click on My Computer (This PC) and click on properties
my computer properties
  • A window will open, from left click on Advanced System Settings 
advance system settings
  • Now from the new window that will open click on environment variables
system properties
system properties
  • Now from the new window in System Variables click on new
system variables
  • Now in Variable Name write ‘JAVA_HOME’ and in value copy the path of your JDK Installation directory usually inside C:\Program Files\Java\jdkxxx
new system variable
  • Now apply the changes and its done.

Installing Android Studio

  • Download Android Studio from here
  • You will again get an executable file to install android studio
  • Install android studio (next -> next -> finish)
  • and thats it so simple
  • Now you can open Android Studio from start menu
  • The final part of this post is to create an emulator to run and debug android apps

Creating an Android Emulator for Debugging and Testing

  • Though the installation of Android Studio itself creates an Android Emulator but for this series of tutorial I will be using GenyMotion
  • GenyMotion is fast and smooth and consumes less memory so I will be using GenyMotion.
  • You can see the details of Installing GenyMotion from here
  • After successfully Installing GenyMotion we can start our Android Application Development Tutorial.

Creating Your First Android Application

  • Open Android Studio
  • Click on Start a New Android Studio Project
welcome to android studio
  • Now you have to fill some details
  • The first thing is name of your application
  • Next is company domain, you can enter any domain it will be use to create the java packages in your project
  • And at last you have to choose a location in your hard drive to store your project
new project
  • After filling out the above details click on next
  • Now you have to select the minimum version of android for your application
  • It is by default 4.0.3 (IceCreamSandwich) that means the application you are creating will not be supported by devices having the earlier versions of android from android 4.0.3
  • Now Click on Next
target android devices
  • Now you have to select an Activity for your Application. In android an activity is the screen you see in the application. You can have multiple activities in your application. For now we are creating an activity.
  • Here you can see a number of predefined activity templates, don’t bother much we are selecting the first one which is Blank Activity.
  • Now click next
  • add an activity
    • Now you will get the option to customize the Activity
    cutomize the activity
    • All the fields are filled already with default so you do not need to change anything unless you don’t want
    • The first thing is Activity Name: It is the name of the activity you are creating. A java file will be created with this name. You can change the name if you want but for now we are not changing the name.
    • Next is Layout Name: In android we separate the Layout Designing and Coding with XML and JAVA. So for your Activity’s visual design a separate layout file will be created. And what you are seeing here is activity_main this is the name of the xml file for your layout. You can also change this but the rule is you can only use small letters and underscores (_) any other characters are not allowed.
    • Next is Title: This is the Title which will appear at the top of your application. Usually it is our Application’s Name.
    • The Last is Menu Resource Name: Android studio will also create a menu resource xml file for the menu of your application. And this is the name of the menu file for your android application.
    • Don’t worry if you are having difficulties understanding these things we will  cover this in detail in my upcoming articles
    • And you can also change these settings after creating the project so don’t bother much and just click on Finish.
    • Now wait for few minutes and you will see the following screen
    android studio project
    • As you can see it is already Hello World!. So now we have an app with a single activity.
    • Don’t bother much now we will try to cover each and everything, but for this post I am wrapping up this tutorial.
    • Now just see how we can run our application. As I have already said we will use GenyMotion. So open GenyMotion from start menu and start your virtual device.
    • Now in Android Studio click on the play button from the top
    • Now wait for a while until don’t see the following window
    choose device
    • Now as you can see I have my Genymotion Virtual Device is running. Select the device and click on ok.
    • See your Virtual Device Now
    genymotion
    Bingo! You have just successfully executed your very first Android Application. So thats it for this post of this Android Application Development Tutorial Series. In upcoming tutorials we will start understanding android application development. So stay tuned and share the post if you liked it. Thank You 🙂

Monday, 14 November 2016

Firebase Cloud Messaging for Android

Creating an Android Studio Project

  • As we do always 😛 create a new android studio project. Once the project is loaded copy the package name you can get it from the AndroidManifest.xml file.

Getting a Configuration File

firebase cloud messaging tutorial
  • Now put your app name and select your country.
firebase cloud messaging tutorial
  • Now click on Add Firebase to Your Android App.
add firebase to android
  • Now you have to enter your projects package name and click on ADD APP.
  • After clicking add app you will get google-services.json file.

Adding Firebase Messaging to Your Project

  • Now come back to your android project. Go to app folder and paste google-services.json file.
firebase cloud messaging tutorial
  • Now go to your root level build.gradle file and add the following code.
  • Inside app level build.gradle file make the following changes.
  • Now sync your project.

Implementing Firebase Cloud Messaging

  • Create a class named  MyFirebaseInstanceIDService.java and write the following code.
  • Now create MyFirebaseMessagingService.java and write the following code.
  • Now we have to define the above services in our AndroidManifest.xml file. So go to manifest and modify as follows.
  • Thats all. Now run your app (Make sure your device is having google play service or else it won’t work).
  • After running you will see the token in the logcat. Copy the token.
firebase cloud messaging tutorial

Sending Push Notification using Firebase Console

  • Go to firebase console and select the app you created.
  • From the left menu select notification.
  • Click on new message.
  • Enter message, select single device and paste the token you copied and click on send. The same as I did on the video, and check your device
firebase cloud messaging tutorial
Firebase Cloud Messaging Tutorial
  • Bingo! It is working absolutely fine. If you are having trouble then you can get my source code from the link given below.
  • I hope you all like it if any Queries please comment it below.