Wednesday 1 June 2016

Profile Image Choosing with cropping

Java:

import android.app.ActionBar; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Parcelable; import android.provider.MediaStore; import android.view.MenuItem; import android.view.View; import com.android.framework.utils.Constants; import com.android.theme.activity.AdminBaseActivity; import com.creatah.employeeattendance.R; import com.theartofdev.edmodo.cropper.CropImageView; import java.io.File; import java.util.ArrayList; import java.util.List; public class ImageCropperActivity extends AdminBaseActivity { private CropImageView mCropImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_cropper); setTitle("Image Cropping"); mCropImageView = (CropImageView) findViewById(R.id.CropImageView); startActivityForResult(getPickImageChooserIntent(), 200); } public void onCropImageClick(View view) { Bitmap cropped = mCropImageView.getCroppedImage(ActionBar.LayoutParams.WRAP_CONTENT, 
ActionBar.LayoutParams.WRAP_CONTENT); if (cropped != null) { Constants.bitmap = cropped; setResult(RESULT_OK); finish(); } else { startActivityForResult(getPickImageChooserIntent(), 200); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { Uri imageUri = getPickImageResultUri(data); mCropImageView.setImageUriAsync(imageUri); Constants.ImagePath = String.valueOf(imageUri); } } public Intent getPickImageChooserIntent() { Uri outputFileUri = getCaptureImageOutputUri(); List<Intent> allIntents = new ArrayList<>(); PackageManager packageManager = getPackageManager(); Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); for (ResolveInfo res : listCam) { Intent intent = new Intent(captureIntent); intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); intent.setPackage(res.activityInfo.packageName); if (outputFileUri != null) { intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); } allIntents.add(intent); } // collect all gallery intents Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT); galleryIntent.setType("image/*"); List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0); for (ResolveInfo res : listGallery) { Intent intent = new Intent(galleryIntent); intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); intent.setPackage(res.activityInfo.packageName); allIntents.add(intent); } // the main intent is the last in the list (fucking android) so pickup the useless one Intent mainIntent = allIntents.get(allIntents.size() - 1); for (Intent intent : allIntents) { if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) { mainIntent = intent; break; } } allIntents.remove(mainIntent); Intent chooserIntent = Intent.createChooser(mainIntent, "Select source"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()])); return chooserIntent; } private Uri getCaptureImageOutputUri() { Uri outputFileUri = null; File getImage = getExternalCacheDir(); if (getImage != null) { outputFileUri = Uri.fromFile(new File(getImage.getPath(), "pickImageResult.jpeg")); } return outputFileUri; } /** * Get the URI of the selected image from {@link #getPickImageChooserIntent()}.<br/> * Will return the correct URI for camera and gallery image. * * @param data the returned data of the activity result */ public Uri getPickImageResultUri(Intent data) { boolean isCamera = true; if (data != null) { String action = data.getAction(); isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE); } return isCamera ? getCaptureImageOutputUri() : data.getData(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); break; default: break; } return super.onOptionsItemSelected(item); } }

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.theartofdev.edmodo.cropper.CropImageView android:id="@+id/CropImageView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" custom:cropAspectRatioX="5" custom:cropAspectRatioY="5" custom:cropFixAspectRatio="true" custom:cropScaleType="fitCenter" custom:cropShape="rectangle" android:layout_margin="10dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/actionbar_admin" android:onClick="onCropImageClick" android:padding="@dimen/activity_horizontal_margin" android:textAllCaps="false" android:text="Apply" android:textColor="@color/white" /> </LinearLayout>

gradle:

    compile 'com.theartofdev.edmodo:android-image-cropper:1.2.+'

No comments:

Post a Comment

Send Whatsapp Message via PHP Code

  How to Send and Receive Messages in WhatsApp using PHP In this tutorial you will see How to Send and Receive Messages in WhatsApp using PH...