I tried to get image from gallery and capture from camera and display image in my imageView using fragment but the onActivityResult() does not response. Below is my code for capturing image from camera or gallery.
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
final Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAPTURE_IMAGE);
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""),
PICK_IMAGE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("OnActivityResult Call");
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == PICK_IMAGE) {
imagePath = getAbsolutePath(data.getData());
myImageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageLoader.displayImage("file://" + imagePath, ivUpload,
options);
} else if (requestCode == CAPTURE_IMAGE) {
imagePath = getImagePath();
System.out.println("IMAGE PATH===" + imagePath);
myImageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageLoader.displayImage("file://" + imagePath, ivUpload,
options);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
private String getAbsolutePath(Uri uri) {
String[] projection = { MediaColumns.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = getActivity().managedQuery(uri, projection, null, null,
null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
private Uri setImageUri() {
File file = new File(Environment.getExternalStorageDirectory()
+ "/DCIM/", "image" + new Date(CAPTURE_IMAGE).getTime()
+ ".png");
Uri imgUri = Uri.fromFile(file);
this.imagePath = file.getAbsolutePath();
return imgUri;
}
private String getImagePath() {
return imagePath;
}
please give me any solution for that fragment. i also try with in activity. it is work but in fragment does not upload image from gallery.
u can set image from gallery
If you have Image path, you can directly display image from image path..
You can write like this...
BitmapFactory.decodeFile()
method allows you to decode image from file path. so you can set decoded image directly to theImageView
bysetImageBitmap()
method.Edit :
Here I am adding sample code for picking intent..
You can take reference and see whats the problem there..
To Call Image Intent
Activity Result
This may help you..
That fragment does the job
With that XML
Override onActivityResult in activity.