I am a beginner in Android.
I am going to make image picker from camera and file explorer.
I found out example code from google search.
But after capture, it don't call onActivityResult() Method.
So I cannot handle the capture result.
note: I am working on Fragment.
public void selectImage(){
final String [] items = new String [] {"Take Photo", "Choose Photo"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(), android.R.layout.select_dialog_item, items);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add/Change Photo");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mImageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
}
catch (Exception e) {
e.printStackTrace();
}
dialog.cancel();
}
else {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getActivity().startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
} );
final AlertDialog dialog = builder.create();
dialog.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode,data);
// TODO Fix no activity available
if (resultCode != getActivity().RESULT_OK)
return;
Bitmap bitmap = null;
String path = "";
if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
path = getRealPathFromURI(mImageCaptureUri); // From Gallery
if (path == null)
path = mImageCaptureUri.getPath(); // From File Manager
if (path != null)
bitmap = BitmapFactory.decodeFile(path);
}
else {
path = mImageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(path);
}
imgProfile.setImageBitmap(bitmap);
super.onActivityResult(requestCode, resultCode, data);
}
calling getActivity().startActivityForResult() will result a call to onActivityResult() on the Activity hosting the Fragment and not the onActivityResult() of the Fragment it self.
If you want a call to onActivityResult() in the Fragment itself call
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
and if you want to handle it it the Activity you need to call:
getActivity().startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
Also you might want to reorganize your onActivityResult() as
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// TODO Fix no activity available
if (resultCode != getActivity().RESULT_OK)
Bitmap bitmap = null;
String path = "";
if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
path = getRealPathFromURI(mImageCaptureUri); // From Gallery
if (path == null)
path = mImageCaptureUri.getPath(); // From File Manager
if (path != null)
bitmap = BitmapFactory.decodeFile(path);
} else {
path = mImageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(path);
}
imgProfile.setImageBitmap(bitmap);
}
In your fragment code you need to call the activity from the fragment. Instead of:
getActivity().startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
you need to call the following, as your code is inside a nested class.
YOUR_FRAGMENT.this.startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
without getActivity.
Related
i have 4 diffrent imageview and try to click on all to upload diffrent images like LIcence,Rc,Profile etc . I want to uploaded image from gallery or camera dialog..
like this layout.
like uper layout example
Please check my updated answer. This is just an example. Hope you understand from this
ImageView profile_pic;
private static final int SELECT_PICTURE1 = 100;
private static final int SELECT_PICTURE2 = 101;
private static final int SELECT_PICTURE3 = 102;
private static final int SELECT_PICTURE4 = 103;
picture1 = (ImageView) view.findViewById(R.id.picture1);
picture2 = (ImageView) view.findViewById(R.id.picture2);
picture3 = (ImageView) view.findViewById(R.id.picture3);
picture4 = (ImageView) view.findViewById(R.id.picture4);
picture1.setOnClickListener(this);
picture2.setOnClickListener(this);
picture3.setOnClickListener(this);
picture4.setOnClickListener(this);
#Override
public void onClick(View v) {
if (v.getId() == R.id.picture1) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE1);
}
if (v.getId() == R.id.picture2) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE2);
}
if (v.getId() == R.id.picture3) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE3);
}
if (v.getId() == R.id.picture4) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE4);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE1) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
pricture1.setImageURI(selectedImageUri);
}
}
if (requestCode == SELECT_PICTURE2) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
picture2.setImageURI(selectedImageUri);
}
}
if (requestCode == SELECT_PICTURE3) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
picture3.setImageURI(selectedImageUri);
}
}
if (requestCode == SELECT_PICTURE4) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
picture4.setImageURI(selectedImageUri);
}
}
}
}
private void imageBrowse() {
if(camera){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}else{
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, PICK_IMAGE_REQUEST);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode == PICK_IMAGE_REQUEST){
Uri picUri = data.getData();
filePath = getPath(picUri);
uri =picUri;
Log.d("picUri", picUri.toString());
Log.d("filePath", filePath);
imageView.setImageURI(picUri);
}
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
EDIT
Since you are using recyclerview or listview kind of view, you can associate tag to each viewholder and then use that tag for resolving imageview. Now you can get unique tag based on position in view. For eg in recyclerview you can use getAdapterPosition().
Associate OnClicks of each imageview with a request code. In onActivityResult resolve them and put images accordingly.
I would like to suggest to refer this site, It's implementation is easy and straight forward...
http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/
In addition to the perfectly working answer by #paras, if someone wants to include the Camera option also then please refer to the below steps:
Add a dialog box which pops-up on image click and asks to chose either Camera or Gallery:
final CharSequence[] items = {"Camera", "Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(PhotoActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Camera")) {
openCamera();
} else if (items[item].equals("Gallery")) {
openGallery();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
Use this method to open the Camera intent:
private void openCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_CAMERA);
}
Now in the onActivityResult() method, include this to check whether the intent is from Camera:
if (requestCode == REQUEST_CAMERA) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) Objects.requireNonNull(bundle).get("data");
imageView.setImageBitmap(bitmap);
}
I want to pick image from the gallery and use it in my App as Profile Picture. So far I've tried this.
Here's my InfoGet class. The loadImagefromGallery gets called on click.
public void loadImagefromGallery(View view) {
/* Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD);*/
Intent gallery_Intent = new Intent(getApplicationContext(), GalleryUtil.class);
startActivityForResult(gallery_Intent, GALLERY_ACTIVITY_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_ACTIVITY_CODE) {
if(resultCode == Activity.RESULT_OK){
String picturePath = data.getStringExtra("picturePath");
//perform Crop on the Image Selected from Gallery
performCrop(picturePath);
}
}
if (requestCode == RESULT_CROP ) {
if(resultCode == Activity.RESULT_OK){
Bundle extras = data.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
// Set The Bitmap Data To ImageView
pro.setImageBitmap(selectedBitmap);
pro.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
}
private void performCrop(String picUri) {
try {
//Start Crop Activity
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 280);
cropIntent.putExtra("outputY", 280);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, RESULT_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
And here's my GalleryUtil code
public class GalleryUtil extends Activity {
private final static int RESULT_SELECT_IMAGE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final String TAG = "GalleryUtil";
String mCurrentPhotoPath;
File photoFile = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
//Pick Image From Gallery
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_SELECT_IMAGE);
}catch(Exception e){
e.printStackTrace();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case RESULT_SELECT_IMAGE:
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
try{
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();
//return Image Path to the Main Activity
Intent returnFromGalleryIntent = new Intent();
returnFromGalleryIntent.putExtra("picturePath",picturePath);
setResult(RESULT_OK,returnFromGalleryIntent);
finish();
}catch(Exception e){
e.printStackTrace();
Intent returnFromGalleryIntent = new Intent();
setResult(RESULT_CANCELED, returnFromGalleryIntent);
finish();
}
}else{
Log.i(TAG, "RESULT_CANCELED");
Intent returnFromGalleryIntent = new Intent();
setResult(RESULT_CANCELED, returnFromGalleryIntent);
finish();
}
break;
}
}
}
I need help can anyone just make some changes in the existing code. I do not want to rebuild anything from scratch. I want to optimise this image picker before I release the next update. The Existing code work okay for devices with decent amount of RAM like 1GB or so.
Please help me.
And also explain what you did and how it works. Thanks a LOT.
I did that method read images from path and solve this issue, maybe it help you:
public static Bitmap readBitmap(Uri selectedImage, int resizeFactor)
{
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = resizeFactor;
AssetFileDescriptor fileDescriptor = null;
try
{
fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
} catch (FileNotFoundException e)
{
e.printStackTrace();
} finally
{
try
{
bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
fileDescriptor.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
return bm;
}
The atributes are Uri selectedImage and int resizeFactor;
on selectedImage you need to put a Path from image and on resizeFactor put a int number, Normally I use number 10, try with that or try with another one (down/up)
Now if you want to put on ImageView you can use that.
ImageView.setImageBitmap(readBitmap(Path, 10));
Later you need clear Bitmap content to avoid OutOfMemory, use that method:
public static void clearBitmap(Bitmap bm)
{
bm.recycle();
}
I am asking the user for the access to the gallery through the code as a listener here:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
However, I am confused as to how I would set a variable to the photo selected.
Where would I put the code to set a variable as the photo selected?
Thanks :)
First you have to override onActivityResult to get the uri of the file selected image
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == SELECT_PHOTO) {
if (resultCode == RESULT_OK) {
if (intent != null) {
// Get the URI of the selected file
final Uri uri = intent.getData();
useImage(uri);
}
}
super.onActivityResult(requestCode, resultCode, intent);
}
}
Then define useImage(Uri) to use the image
void useImage(Uri uri)
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
//use the bitmap as you like
imageView.setImageBitmap(bitmap);
}
You can do it like this.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Here we need to check if the activity that was triggers was the Image Gallery.
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
// If the resultCode is RESULT_OK and there is some data we know that an image was picked.
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
// Do something with the bitmap
// At the end remember to close the cursor or you will end with the RuntimeException!
cursor.close();
}
}
Alternative for Akash Kurian Jose answer
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
I always use
fun getBitmap(file: Uri, cr: ContentResolver): Bitmap?{
var bitmap: Bitmap ?= null
try {
val inputStream = cr.openInputStream(file)
bitmap = BitmapFactory.decodeStream(inputStream)
// close stream
try {
inputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}catch (e: FileNotFoundException){}
return bitmap
}
It works both for photos from gallery and photos from camera.
Larger issue about it: Picasso unable to display image from Gallery
Open Gallery using this method:
private void openGallery(){
if (Build.VERSION.SDK_INT <19){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
}
}
Then you are able to read convert Uri to Bitmap using afromentioned ContentResolver.openInputStream or set image ImageView.setImageUri(Uri)
If you want to display the selected image to any particular ImageView.
Suppose we have RC_PHOTO_PICKER = 1 then these lines of code should do the magic
private void openPhotoPicker() {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, false);
startActivityForResult(Intent.createChooser(photoPickerIntent,"Complete Action Using"), RC_PHOTO_PICKER);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
//set the selected image to ImageView
mImageView.setImageURI(pickedImage);
}
}
And simply call the openPhotoPicker() method afterwards
I am able to capture an image from gallery or camera and put it in imageview with following code:
1. Capture image from gallery or camera:
private void captureImageInitialization() {
final String[] items = new String[] { "Take from camera",
"Select from gallery" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item, items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Image");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { // pick from
// camera
if (item == 0) {
Intent intent = new Intent(
"android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, PICK_FROM_CAMERA);
} else {
// pick from file
/**
* To select an image from existing files, use
* Intent.createChooser to open image chooser. Android will
* automatically display a list of supported applications,
* such as image gallery or file manager.
*/
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_FILE);
}
}
});
dialog = builder.create();
}
2. Show in imageview:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case PICK_FROM_CAMERA:
mImageCaptureUri = data.getData();
imagepath = getPath(mImageCaptureUri);
bitmap = BitmapFactory.decodeFile(imagepath);
mImageView.setImageBitmap(bitmap);
// Log.d("camera ---- > ", "" + data.getExtras().get("data"));
new ProcessUploadAvatar2().execute();
break;
case PICK_FROM_FILE:
/**
* After selecting image from files, save the selected path
*/
mImageCaptureUri = data.getData();
imagepath = getPath(mImageCaptureUri);
bitmap2 = BitmapFactory.decodeFile(imagepath);
mImageView.setImageBitmap(bitmap2);
break;
}
}
Now my question is how to check if the image in the imageview has been taken by camera or gallery beyond the onActivityResult() method? I try to implement something like this:
public void NetAsync() {
if (mImageView.getDrawable() != null) {
if (mImageView = bitmap) {
new NetCheck().execute();
} else if (mImageView = bitmap2) {
new ProcessUploadAvatar2().execute();
}
}
}
You can easily get this via single check of request-codes in onActivityResult()... I mean those request-codes which you've sent while calling camera/gallery intent..
hi i am not able to getting call to method onActivityResult after below code.
private void ImageChooseOptionDialog() {
Log.i(TAG, "Inside ImageChooseOptionDialog");
String[] photooptionarray = new String[] { "Take a photo",
"Choose Existing Photo" };
Builder alertDialog = new AlertDialog.Builder(TabSample.tabcontext)
.setItems(photooptionarray,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
Log.i(TAG, "Which" + which);
Log.i(TAG,
"Inside ImageChooseOptionDialog Camera");
_path = Environment
.getExternalStorageDirectory()
+ File.separator
+ "TakenFromCamera.png";
Log.d(TAG, "----- path ----- " + _path);
media = _path;
// File file = new File(_path);
// Uri outputFileUri = Uri.fromFile(file);
// Intent intent = new Intent(
// android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// intent.putExtra(MediaStore.EXTRA_OUTPUT,
// outputFileUri);
// startActivityForResult(intent, 1212);
} else if (which == 1) {
Log.i(TAG, "Which" + which);
Log.i(TAG,
"Inside ImageChooseOptionDialog Gallary");
// Intent intent = new Intent();
// intent.setType("image/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
// startActivityForResult(Intent
// .createChooser(intent,
// "Select Picture"), 1);
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent
.createChooser(intent,
"Select Picture"), 1);
Log.i(TAG, "end" + which);
}
}
});
alertDialog.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.create().show();
}
and this is my onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Inside Onactivity Result");
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Log.i(TAG, "Inside if else Onactivity Result");
// currImageURI is the global variable I’m using to hold the
// content:// URI of the image
Uri currImageURI = data.getData();
String path = getRealPathFromURI(currImageURI);
Log.i(TAG, "Inside Onactivity Result Image path" + path);
}
}
}
please let me knew where i am doing wrong. i am calling onActivityResult method from which=1 after dialog box appears.
but not getting any log inside onActivityResult method in logcat.
This is because, you may not be getting RESULT_OK. Always first check for request code and inside that check for result code. If you are retrieving image from gallery, try following code inside onActivityResult():
if (requestCode == TAKE_PICTURE_GALLERY) {
if (resultCode == RESULT_OK) {
final Uri selectedImage = data.getData();
final String[] filePathColumn = { MediaStore.Images.Media.DATA };
final Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
final int columnIndex = cursor
.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
cursor.close();
}
}
And use the filePath wherever you want.
I hope this solves your problem. Thank you :)
UPDATE:
Use this code to start your gallery activity:
imagePathURI = Uri.fromFile(new File(<your image path>));
final Intent intent = new Intent(Intent.ACTION_PICK, imagePathURI);
intent.setType("image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imagePathURI);
startActivityForResult(intent, TAKE_PICTURE_GALLERY);
When you want to retrieve image from gallery, the MediaStore.EXTRA_OUTPUT refers to The name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video. So here, you have to pass the image path where u'll receive your image.
imagePathURI = Uri.fromFile(new File(<your image path>)); // here a file will be created from your image path. And when you'll receive an image, u can access the image from this image path.