Step 1: create the activity.xml
<ImageView android:id="@+id/lmageviews" android:layout_width="120dp" android:layout_height="120dp" android:layout_gravity="center|bottom" android:layout_marginLeft="20dp" android:background="@drawable/bg" android:clickable="true" android:onClick="avtar" android:padding="5dp" />
step 2: manifest:<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/
step 3: dependencies:
compile 'com.kbeanie:image-chooser-library:1.5.2@aar'
compile 'com.github.bumptech.glide:glide:3.6.0'
Step 4:
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.kbeanie.imagechooser.api.ChooserType;
import com.kbeanie.imagechooser.api.ChosenImage;
import com.kbeanie.imagechooser.api.ImageChooserListener;
import com.kbeanie.imagechooser.api.ImageChooserManager;
import java.io.File;
import java.io.FileOutputStream;
/**
* Created by DHYAN on 9/15/2016.
*/
public class RegistrationActivity extends AppCompatActivity implements ImageChooserListener {
private final String TAG = "tag";
ImageView imgview;
EditText editBirthdate;
private String originalFilePath;
private boolean is_image_chosen = false;
private String thumbnailFilePath;
private ImageChooserManager imageChooserManager;
private String filePath;
private int chooserType;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reg_screen);
imgview = (ImageView) findViewById(R.id.lmageviews);
/* editBirthdate = (EditText) findViewById(R.id.edtBirtdate);
editBirthdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
}
});
*/
}
/************* START DATE PICKER DIALOG **************/
private void showDatePickerDialog() {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
newFragment.setCallBack(mDateListener);
}
private DatePickerFragment.DateListener mDateListener = new DatePickerFragment.DateListener() {
@Override
public void onDateSelected(int year, int month, int day) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.set(year, month, day);
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy");
String formattedDate = sdf.format(c.getTime());
editBirthdate.setText(formattedDate);
}
};
/************* END DATE PICKER DIALOG **************/
/************* Start Image Picker DIALOG *************/
public void avtar(View view) {
showImagePickerDialog();
}
private void showImagePickerDialog() {
CharSequence[] items = {"From Camera", "From Gallery"};
CharSequence[] items_add = {"From Camera", "From Gallery", "Remove photo"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Image");
builder.setItems(is_image_chosen ? items_add : items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
switch (which) {
case 0:
takePicture();
break;
case 1:
chooseImage();
break;
case 2:
is_image_chosen = false;
imageChooserManager.clearOldFiles();
imgview.setImageResource(R.drawable.bg);
break;
}
}
});
final AlertDialog dialog = builder.create();
dialog.show();
}
private void chooseImage() {
chooserType = ChooserType.REQUEST_PICK_PICTURE;
imageChooserManager = new ImageChooserManager(this,
ChooserType.REQUEST_PICK_PICTURE, true);
imageChooserManager.setImageChooserListener(this);
imageChooserManager.clearOldFiles();
try {
filePath = imageChooserManager.choose();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private void takePicture() {
chooserType = ChooserType.REQUEST_CAPTURE_PICTURE;
imageChooserManager = new ImageChooserManager(this,
ChooserType.REQUEST_CAPTURE_PICTURE, true);
imageChooserManager.setImageChooserListener(this);
try {
filePath = imageChooserManager.choose();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onImageChosen(final ChosenImage image) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "Chosen Image: O - " + image.getFilePathOriginal());
Log.i(TAG, "Chosen Image: T - " + image.getFileThumbnail());
Log.i(TAG, "Chosen Image: Ts - " + image.getFileThumbnailSmall());
is_image_chosen = true;
originalFilePath = image.getFilePathOriginal();
thumbnailFilePath = image.getFileThumbnail();
//thumbnailSmallFilePath = image.getFileThumbnailSmall();
// progressBar.setVisibility(View.GONE);
if (image != null) {
Log.i(TAG, "Chosen Image: Is not null");
loadImage(image.getFileThumbnail());
//loadImage(imageViewThumbSmall, image.getFileThumbnailSmall());
} else {
Log.i(TAG, "Chosen Image: Is null");
}
}
});
}
@Override
public void onError(String s) {
}
private void loadImage(String path) {
Glide.with(this).load(new File(path)).centerCrop().into(imgview);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "OnActivityResult");
Log.i(TAG, "File Path : " + filePath);
Log.i(TAG, "Chooser Type: " + chooserType);
if (resultCode == RESULT_OK
&& (requestCode == ChooserType.REQUEST_PICK_PICTURE
|| requestCode == ChooserType.REQUEST_CAPTURE_PICTURE)) {
if (imageChooserManager == null) {
reinitializeImageChooser();
}
imageChooserManager.submit(requestCode, data);
} else {
}
}
// Should be called if for some reason the ImageChooserManager is null (Due
// to destroying of activity for low memory situations)
private void reinitializeImageChooser() {
imageChooserManager = new ImageChooserManager(this, chooserType, true);
imageChooserManager.setImageChooserListener(this);
imageChooserManager.reinitialize(filePath);
}
private File getResizeImage(String path) {
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
Bitmap b = BitmapFactory.decodeFile(path);
Bitmap out = Bitmap.createScaledBitmap(b, 250, 250, false);
File file = new File(dir, "resize_profile.png");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
b.recycle();
out.recycle();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
No comments:
Post a Comment