Tuesday 16 December 2014

Loading Image after cropping


Hi Friends,

In this blog i will make a simple app in which one image view is here by clicking image view a dialog will popup with option "Take Photo", "Choose from Library" and "Cancel".

In Take Photo user will click a photo to set on ImageView.
In Choose from Library user will allow to set image from library.

Here is the code.....

In the activity_main.xml file I simple take a ImageView....
Just paste the following in your code.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

<--Here i took a default image which is set on ImageView dy default-->
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/default_image" />

</LinearLayout>

Here comes the main code.
In MainActivity.java File paste the following code

package in.blogspot.longjamcode;

import java.io.File;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);

//here I set OnClickListener for ImageView
imageView.setOnClickListener(imageClickListener);
}

//here is imageClickListener when user click imageView this listener will called
private OnClickListener imageClickListener = new OnClickListener() {
@Override
public void onClick(View v) {

//A dialog will popup to user to choose their preference 
final CharSequence[] items = { "Take Photo", "Choose from Library","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
//intent to start up camera 
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

//on finished it will called the onActivityResult callback method with request code 101
startActivityForResult(intent, 101);
} else if (items[item].equals("Choose from Library")) {

//intent to pick image from library
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

//on finished it will called the onActivityResult callback method with request code 102
startActivityForResult(i, 102);
} else if (items[item].equals("Cancel")) {

//This will simply dismiss the dialog
dialog.dismiss();
}
}
});
builder.show();
}
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap photo = null;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101 && resultCode == RESULT_OK ) {
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
try {
cropCapturedImage(Uri.fromFile(file));
}
catch(ActivityNotFoundException aNFE){
}
}
else if (requestCode == 102 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
File file = new File(picturePath);
try {
cropCapturedImage(Uri.fromFile(file));
}
catch(ActivityNotFoundException aNFE){
}
}
else if(requestCode == 2 && resultCode == RESULT_OK && data != null) {
Bundle extras = data.getExtras();
photo = extras.getParcelable("data");

//here set image on imageView
imageView.setImageBitmap(photo);
}

}

//Method  to crop Image
public void cropCapturedImage(Uri picUri){
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
//this will called the onActivityResult callback method with request code 2
startActivityForResult(cropIntent, 2);
}
}

The output will be like...























For full project Click here

2 comments: