I know there are lots of question related to my question, but i dint get any answer who will solve my problem, Basically my requirement is choose the image from gallary and set that image back to ImageView of child activity of Tab Activity, but in TabActivity i unable to get call to the onActivityResult() method, Since yesterday i was trying to search another way to solve the issue, as i found onActivityResult() will not be going to work i tried to pass image using bundle but i was getting !!! FAILED BINDER TRANSACTION !!! error, how do i handle above situations, please suggest me way to call onActivityResult() method into child activity of TabActivity, Thanks in advance.
My Code is
public void openGallary(int req_code) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, req_code);
}
Here is my onActivityResult() method in which i passed requestcode from openGallary() method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& data != null) {
Uri pickedImage = data.getData();
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]));
imgShowLocationImage.setImageBitmap(BitmapFactory
.decodeFile(imagePath));
cursor.close();
}}
Make this on button pick this will take you to gallery where you will be able to pick image.
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, LOAD_IMAGE_RESULTS);
And then you call onActivityResult like this .
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null)
{
Uri pickedImage = data.getData();
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]));
// image.setImageBitmap(BitmapFactory.decodeFile(imagePath));
image.setImageBitmap(BitmapFactory.decodeFile(imagePath));
cursor.close();
}
This way picked image will apper in your ImageView.
Related
I have a method of retrieving an image from the gallery that works perfectly fine for me. My issue is, I use it in three separate activities and feel that I should make a class that can be called to get the image.
This is my code:
private void getImage () {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
and
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mRLMenuParams.setVisibility(View.GONE);
mRLImageParams.setVisibility(View.VISIBLE);
bmSelectedImage = null;
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imSelectedImage = (ImageView) findViewById(R.id.imageView_image);
imSelectedImage.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
Bitmap bmImage = Bitmap.createBitmap(BitmapFactory.decodeFile(imgDecodableString));
bmSelectedImage = bmImage.copy(Bitmap.Config.ARGB_8888, true);
} else {
Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
When I try to put this in its own class, i can't get it to work, even when passing the Context. It seems that I would need to make it an activity, and if so, I don't see the point in separating it. Am I missing something that will allow me to put this in its own class and return the image?
Thanks!
You're using the method onActivityResult which comes from Activity. Maybe have a class method to do the code you have inside onActivityResult. Change your class to contain two separate methods, since if your separate class contains onActivity it won't work unless it's an activity.
http://developer.android.com/reference/android/app/Activity.html
I am developing an android application for the first time and I was wondering how can I have a option of having the user upload an image. Like for example, in a contact manager the user has the option of uploading an image of a contact. I was wondering how can I do that in my android application. Any help would be appreciated.
You can start from scratch..
1)Take a look at INTENT
Android Intent
Then take a look at this blogpost
Image Upload
Hope this helps you
In this one you want to click the image view to get image from sd card.. If you want to change to button then replace the listener from image to button.
int RESULT_LOAD_IMAGE = 1;
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
getting image from sd card:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
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();
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
I in see many apps where the user can upload a profile picture, there is an empty frame with a plus in it. where the user clicks and then it loads the gallary etc.
It seems pretty universal on how it looks and behaves with a thumbnail loading inside the image etc so I am wondering if there is a widget or custom view that these apps use. if not then can someone give me an idea on how to implement this from scratch.
Make the empty imageview clickable with onClick method containing sth like this:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 100 );
Then in
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
uiHelper.onActivityResult(requestCode, resultCode, data);
if(requestCode == 100 && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User picked an image.
Cursor cursor = getActivity()
.getContentResolver()
.query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
//Link to the image
final String imageFilePath = cursor.getString(0);
myAvatar = BitmapFactory.decodeFile(imageFilePath);
ImageView iv = (ImageView) yourView.findViewById(R.id.yourview);
iv.setImageBitmap(myAvatar);
cursor.close();
}
super.onActivityResult(requestCode, resultCode, data);
}
I'm using an intent like this:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
And in onActivityResult() I have this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return; // user cancelled
}
Uri imageUri = data.getData();
if (imageUri == null) {
// (code to show error message goes here)
return;
}
// Get image path from media store
String[] filePathColumn = { android.provider.MediaStore.MediaColumns.DATA };
Cursor cursor = this.getContentResolver().query(imageUri, filePathColumn,
null, null, null);
if (cursor == null || !cursor.moveToFirst()) {
// (code to show error message goes here)
return;
}
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imagePath = cursor.getString(columnIndex);
cursor.close();
if (imagePath == null) {
// error happens here
}
}
When I select images from particular albums like "Posts", "Profile Photos" (see screenshot) I'm unable to get the image path in onActivityResult(). Images from other albums can be selected with no problems.
I've tried adding intent.putExtra("return-data", true) but data.getExtras() returns null in onActivityResult().
There is similar question here, but no one answered it.
Please help!
hops this will helps you ....
ACTIVITYRESULT_CHOOSEPICTURE is the int you use when calling startActivity(intent, requestCode);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITYRESULT_CHOOSEPICTURE) {
BitmapFactory.Options options = new BitmapFactory.Options();
final InputStream ist = ontext.getContentResolver().openInputStream(intent.getData());
final Bitmap bitmap = BitmapFactory.decodeStream(ist, null, options);
ist.close();
}
}
if above code doesn't work than just refer this link... it will surly shows the way
http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/
try this:
String selectedImagePath = imageUri.getEncodedPath();
it works for me using gallery image picker
maybe this:
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
This is my first strange result that I never expected. This is may be lack of skill in that area.
Well I had a button, through which I need to select an image from the phone gallery (probably from sdcard). I used implicit intent to call the phone gallery and got the absolute image path with startActivityForResult(). Immediately am calling another activity putting that path with startActivity().
According to my scenario I wrote the following code in the onClick() of button.
#Override
public void onClick(View v) {
upLoadPhoto();
}
protected void upLoadPhoto() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("return-data", true);
System.out.println("select image");
startActivityForResult(intent, 1);
startActivity(next);
finish();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && data != null && data.getData() != null){
Uri uri = data.getData();
if (uri != null) {
Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
final String imageFilePath = cursor.getString(0);
System.out.println("Background : "+imageFilePath);
next.putExtra("backImagePath", imageFilePath);
cursor.close();
super.onActivityResult(requestCode, resultCode, data);
}
}
}
When I click the button, startActivity(next) is called first, then startActivityForResult(intent,1) is called. As am trying to get image path in the second activity through bundle object, am getting NullPointerException because of startActivity(next) is being called first.
I dropped my jaws when I saw my debugging point are not as expected. Hope I get exact reason to this issue.
Thanks
Aswin
What about moving the call to startActivity(next); into onActivityResult()... this way you will navigate to the other activity after getting the path
protected void upLoadPhoto() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("return-data", true);
System.out.println("select image");
startActivityForResult(intent, 1);
finish();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && data != null && data.getData() != null){
Uri uri = data.getData();
if (uri != null) {
Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
final String imageFilePath = cursor.getString(0);
System.out.println("Background : "+imageFilePath);
next.putExtra("backImagePath", imageFilePath);
cursor.close();
startActivity(next);
super.onActivityResult(requestCode, resultCode, data);
}
}
}
You have call startActivity() inside onActivityResult() when your image is selected from Gallery successful. Because when startActivityForResult() it is going to Gallery for picking up the image and till then startActivity() is fired and you move to next Activity.
remove that startActivity(next) from that function and put it on onActivityResult after you retrieve your data
remove that startActivity(next) from that function and put it on onActivityResult after get 1 add Your Next Intent