android how to call onactivityresult with tabhost activity? - android

i am using tabs at bottom on all screens.,and i am using onactivityresult() method for selecting image from gallery and camera.my code is working without tab activity.,but when i am using my code with tabactivity its not working,i am searching from 2 days ,but no solution is getting.,what i do.., i am so frusted from this,please help me..,thankyou.here is onactivityresult()method:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode ==RESULT_OK)
{
Uri chosenImageUri = data.getData();
String imagepath= getpath(chosenImageUri);
Bitmap bitmap = BitmapFactory.decodeFile(imagepath);
customImageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 120, 120, false));
Constant._profilePicBase64=Constant.convertBitmapToBase64(bitmap);
getprofilepic();
}else{
Toast.makeText(SettingsActivity.this, "Unable to get Image", Toast.LENGTH_SHORT).show();
}
break;
case 2:
if (resultCode ==RESULT_OK)
{
try{
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
customImageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 120, 120, false));
Constant._profilePicBase64=Constant.convertBitmapToBase64(bitmap);
getprofilepic();
}catch (OutOfMemoryError e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}else{
Toast.makeText(SettingsActivity.this, "Unable to get Image", Toast.LENGTH_SHORT).show();
}
break;
}
}

I might be late but anyway this is the answer:
your onActivityResult should be placed in the TabActivity group
and your startActivityForResult should be called referring to the parent Activity
example:
TabActivityGroup parentActivity = (TabActivityGroup) getParent();
Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
parentActivity.startActivityForResult(gallery, IMAGE_PICKER_REQUEST);

Related

How take a picture with a Fragment (Android Studio)

I'm trying to take a picture in gallery, so i know do this in Activity, i use a Intent to call the gallery, and onActivityResult for take the path, but when i use a Fragment, i cannot to use "onActivityResult", can someone give a example of it using a Fragment and CustomDialog?
Inside your fragment write this code
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// ******** code for crop image
i.putExtra("crop", "true");
i.putExtra("aspectX", 100);
i.putExtra("aspectY", 100);
i.putExtra("outputX", 256);
i.putExtra("outputY", 356);
try {
i.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(i, "Select Picture"), 0);
}catch (ActivityNotFoundException ex){
ex.printStackTrace();
}
In you Main Activity of the fragment write this code onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0 && resultCode == Activity.RESULT_OK){
try {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
img_user.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Try with this, it should work. Don't forget to accept the answer if correct.

Fetching Captured Image sent through Intent

I'm working on a simple project, where I'm suppose to create an application for taking notes. I've sucessfully created the app but now I want to make it possible for the user to take a photo and save it with the note.
I can take the picture, preview it, and then click save.
But the problem is that I'm not able to get the image sent through the intent to my MainActivity class, where the note is suppose to be saved.
So my question is how do I save the note with the picture, please look at the picture that represents how I want my customRow to look like
I have no idea why this happends beacuse I get no errors... And its been driving me nuts.
Any ideas??
Image.class
public void capturePhoto(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode){
case CAPTURE_IMAGE:
previewCapturedImage();
break;
}
}
else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
private void previewCapturedImage() {
try {
// bitmap factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 2;
bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
//Set image here
imageView.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public void sendImageAsIntent(Boolean isSending){
if(isSending = false){
isSending = true;
}
if(isSending = true) {
Intent i = new Intent(CheckOutMemo.this, MainActivity.class);
i.putExtra("filePath", fileUri.getPath());
setResult(RESULT_OK,i);
finish();
}
}
MainActivity.class
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == Image.ADD_REQUEST_CODE) {
String header = data.getStringExtra("header");
String bodyText = data.getStringExtra("bodyText");
filePath = data.getStringExtra("filePath");
sourceFile = new File(filePath);
fileName = data.getStringExtra("filePath");
Uri imageUri = data.getData();
if(filePath!=null) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
imageIcon.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, "FileNoutFoundException MainActivity", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(MainActivity.this, "IOException MainActivity", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
Memo memo = new Memo(header, bodyText,imageIcon);
customAdapter.add(memo);
customAdapter.notifyDataSetChanged();
} else if (requestCode == Image.EDIT_REQUEST_CODE) {
int position = data.getIntExtra("position", 0);
Memo memo = customAdapter.getItem(position);
memo.header = data.getStringExtra("header");
memo.bodyText = data.getStringExtra("bodyText");
customAdapter.notifyDataSetChanged();
}
}
When you do:
Intent i = new Intent(CheckOutMemo.this, MainActivity.class);
i.putExtra("filePath", fileUri.getPath());
startActivity(i);
You are basically asking to create a new instance of MainActivity. To return to the previous activity while setting the result do this instead:
setResult(RESULT_OK, i);
finish();//finishing activity

How to print an image in a new layout

i tried to print a image in a new layout but it doesn't work
someone can help me?
here the code:
in mainActivity:
case R.id.gallery: //in the moment that the user pressed the button pickAPic, the app will give him to choose a picture
startActivity(new Intent(MainActivity.this, Main2Activity.class));
Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //the app ask access for the gallery
startActivityForResult(pickImageIntent, REQUEST_PICK_PIC); // when he press on a pic
break;
in main2activity:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case REQUEST_PICK_PIC:
if (resultCode == RESULT_OK) {
Uri imageUri = intent.getData();
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
iv.setImageBitmap(bitmap); // the app "prints" the image that the user choose
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
so what is my problem?
thanks you for help
You have to start your main2activity after handle the onActivityResult from your mainActivity like :
mainActivity:
case R.id.gallery: //in the moment that the user pressed the button pickAPic, the app will give him to choose a picture
Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //the app ask access for the gallery
startActivityForResult(pickImageIntent, REQUEST_PICK_PIC); // when he press on a pic
break;
...
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case REQUEST_PICK_PIC:
if (resultCode == RESULT_OK) {
Uri imageUri = intent.getData();
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("IMAGE_URI", uri.toString());
startActivity(intent);
}
break;
}
}
in main2activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Uri uri = Uri.parse(getIntent().getStringExtra("IMAGE_URI"));
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
iv.setImageBitmap(bitmap); // the app "prints" the image that the user choose
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Fragment getting closed after on activity result call

I have a fragments A and B. A contains a list and B has an imageview. when i click on a list item in fragment A it goes to B. I'm calling camera and gallery intent from B.
In B
alert.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, PICK_FROM_CAMERA);
} else if (item == 1) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, PICK_FROM_FILE);
} else {
dialog.cancel();
}
}
});
onActivityResult in B
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_CAMERA) {
if (resultCode == getActivity().RESULT_OK) {
bitmap = (Bitmap) data.getExtras().get("data");
cameraIcon.setImageBitmap(bitmap);
} else if (resultCode == getActivity().RESULT_CANCELED) {
Toast.makeText(getActivity(), "Result has been cancelled!",
Toast.LENGTH_LONG).show();
}
} else if (requestCode == PICK_FROM_FILE) {
try {
if (resultCode == getActivity().RESULT_OK) {
try {
stream = getActivity().getContentResolver()
.openInputStream(data.getData());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap = BitmapFactory.decodeStream(stream);
cameraIcon
.setImageBitmap(Bitmap.createScaledBitmap(bitmap,
bitmap.getWidth() / 2,
bitmap.getHeight() / 2, true));
} else if (resultCode == getActivity().RESULT_CANCELED) {
Toast.makeText(getActivity(), "Result has been cancelled!",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
}
}
}
But some times after the intent collects the image and comes to onActivityResult fragment B
get closed and goes to fragment A instead of setting the image to the image view in fragment B.....
What am i doing wrong please help
Please check using another device.I think the device issue.
Just a guess, but I would say an exception is being thrown in fragment B after onActivityResult has finished.. perhaps you should check your event log/trace the events in the onResume/createView etc.

How to upload image from gallery in android

I want to upload image from my phone gallery into my application .In my application there is button named upload. when i click button,it should move to gallery and in gallery if i select image that selected image should display as thumbnail in application.I want to upload 10 images, from gallery in my application.
On click of the gallery button, start startActivityForResult as follows:
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);
Consequently, detect GET_FROM_GALLERY (which is a static int, any request number of your choice e.g., public static final int GET_FROM_GALLERY = 3;) inside onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Detects request codes
if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
To view gallery:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),REQUEST_CODE);
and to use it in your app:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
//data gives you the image uri. Try to convert that to bitmap
break;
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e(TAG, "Selecting picture cancelled");
}
break;
}
} catch (Exception e) {
Log.e(TAG, "Exception in onActivityResult : " + e.getMessage());
}
}
This is the way to go:
startActivityForResult(
new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
),
GET_FROM_GALLERY
);

Categories

Resources