button with image view problems - android

I have created a fragment which a button and an image view in it. on button click the camera opens when I take a picture then I want that picture to appear in the imageView.
how can I activate this task?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_new_visitor, container, false);
capture=rootview.findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
}
});
return rootview;
please help

Just try this one,
Declare this before on create method
private final int requestCode = 20;
inside onclick event, do this
Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photoCaptureIntent, requestCode);
then override oncreate method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK){
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
imageview.setImageBitmap(bitmap);
}
}

public class BlankFragment extends Fragment {
Button btnGallery,btnCamera;
public static final int GALLEY_REQUEST_CODE = 10;
private static final int CAMERA_REQUEST = 100;
private String TAG = MainActivity.class.getSimpleName();
ImageView image;
private Uri realUri;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
#Override
public void onStart() {
super.onStart();
btnGallery = (Button)getView().findViewById(R.id.btnGallery);
btnCamera = (Button)getView().findViewById(R.id.btnCamera);
image = (ImageView)getView().findViewById(R.id.image);
btnGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(openGallery, "Open Gallery"), GALLEY_REQUEST_CODE);
}
});
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLEY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
image.setImageURI(data.getData()); // set image to image view
try{
// Get real path to make File
realUri = Uri.parse(getRealPathFromURI(data.getData()));
Log.d(TAG,"Image path :- "+realUri);
}
catch (Exception e){
Log.e(TAG,e.getMessage());
}
}
if(requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK){
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
image.setImageBitmap(photo);
}
}
}
public String getRealPathFromURI(Uri contentUri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(contentUri, proj, null, null, null);
assert cursor != null;
if(cursor.moveToFirst()){;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
}

Related

Displaying different file chooser selections on different Imageviews

I have an activity with 5 ImageViews. I have set on click listeners for all of these views. When an image view is clicked, a file chooser is opened and once an image is picked it should be displayed in the imageview that was initially clicked. My problem is that i do not know how to identify which imageview was clicked and thus display the image on the correct view. Below is my code which, at the moment, can only display the chosen image on a 'hard-coded' view.
public class event_creator_view_activity extends AppCompatActivity {
private ImageView display, img1,img2,img3,img4;
private Uri imageuri;
private static final int PICK_IMAGE_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_creator_view_activity);
display = findViewById(R.id.creator_display_image);
img1 = findViewById(R.id.creator_img1);
img2 = findViewById(R.id.creator_img2);
img3 = findViewById(R.id.creator_img3);
img4 = findViewById(R.id.creator_img4);
display.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openfileChooser();
}
});
img1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openfileChooser();
}
});
img2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openfileChooser();
}
});
img3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openfileChooser();
}
});
img4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openfileChooser();
}
});
}
private void openfileChooser(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
private String getFileExtension(Uri uri){
ContentResolver cR = this.getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(cR.getType(uri));
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==PICK_IMAGE_REQUEST&&resultCode==this.RESULT_OK&&data!=null&&
data.getData()!=null){
imageuri = data.getData();
Picasso.get().load(imageuri).into(display);
// uploadFile();
}
}
}
public class event_creator_view_activity extends AppCompatActivity {
private ImageView display, img1,img2,img3,img4;
private Uri imageuri;
private static final int PICK_IMAGE_REQUEST_ONE = 1;
private static final int PICK_IMAGE_REQUEST_TWO = 2;
private static final int PICK_IMAGE_REQUEST_THREE = 3;
private static final int PICK_IMAGE_REQUEST_FOUR = 4;
private static final int PICK_IMAGE_REQUEST_FIVE = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_creator_view_activity);
display = findViewById(R.id.creator_display_image);
img1 = findViewById(R.id.creator_img1);
img2 = findViewById(R.id.creator_img2);
img3 = findViewById(R.id.creator_img3);
img4 = findViewById(R.id.creator_img4);
display.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openfileChooser(PICK_IMAGE_REQUEST_ONE);
}
});
img1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openfileChooser(PICK_IMAGE_REQUEST_TWO);
}
});
img2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openfileChooser(PICK_IMAGE_REQUEST_THREE);
}
});
img3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openfileChooser(PICK_IMAGE_REQUEST_FOUR);
}
});
img4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openfileChooser(PICK_IMAGE_REQUEST_FIVE);
}
});
}
private void openfileChooser(int requestcode){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, requestcode);
}
private String getFileExtension(Uri uri){
ContentResolver cR = this.getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(cR.getType(uri));
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==PICK_IMAGE_REQUEST_ONE&&resultCode==this.RESULT_OK&&data!=null&&
data.getData()!=null){
imageuri = data.getData();
Picasso.get().load(imageuri).into(display);
// uploadFile();
}else if(requestCode==PICK_IMAGE_REQUEST_TWO&&resultCode==this.RESULT_OK&&data!=null&&
data.getData()!=null){
imageuri = data.getData();
Picasso.get().load(imageuri).into(img1);
}else if(requestCode==PICK_IMAGE_REQUEST_THREE&&resultCode==this.RESULT_OK&&data!=null&&
data.getData()!=null){
imageuri = data.getData();
Picasso.get().load(imageuri).into(img2);
}else if(requestCode==PICK_IMAGE_REQUEST_FOUR&&resultCode==this.RESULT_OK&&data!=null&&
data.getData()!=null){
imageuri = data.getData();
Picasso.get().load(imageuri).into(img3);
}else if(requestCode==PICK_IMAGE_REQUEST_FIVE&&resultCode==this.RESULT_OK&&data!=null&&
data.getData()!=null){
imageuri = data.getData();
Picasso.get().load(imageuri).into(img4);
}
}
}
I HOPE IT HELPS...

sd card photos not displayed when picking them from Image gallery , Android studio,

I am using this code to pick an image from gallery and display it in an imageView, the problem that this is working just for photos in internal storage, and when I pick an image from my sd card it's not displayed in my imageView
I hope you have a solution.
public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView) ;
btn = findViewById(R.id.button) ;
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openGallery() ;
}
});
}
public void openGallery(){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
startActivityForResult(intent,PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode==PICK_IMAGE && data != null){
imageUri = data.getData() ;
img.setImageURI(imageUri);
}else{
Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}
}
I am using those permissions in my manifest file :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
I asked and I will answer, the problem was that most of my sd card photos are 2.0 MB or more and they can't be treated directly by the imageView so I added some lines in onActivityResult()
public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView) ;
btn = findViewById(R.id.button) ;
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openGallery() ;
}
});
}
public void openGallery(){
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
startActivityForResult(intent,PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode==PICK_IMAGE && data != null){
imageUri = data.getData() ;
Bitmap bitmap ;
try {
bitmap=MediaStore.Images.Media
.getBitmap(this.getContentResolver(),imgUri);
Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, 160, 160, true);
img.setImageBitmap(mBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}else{
Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}
I hope this will be helpful for those who can face the same problem

Facing issue in getting image from gallery and displaying it in Image-view in fragment

I am working in Fragment in which I need to select an image from
gallery and set it to an imageView. Code is running well, no crashes, no error, but image is not coming into ImageView.
Here is my code :-
public class ProfileOzoneSetting extends Fragment implements OnClickListener
{
Boolean setImg ;
NetworkImageView genSetOzoneUserImage;
private static int RESULT_LOAD = 1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.profiesettingozone, container, false);
genSetOzoneUserImage = (NetworkImageView)rootView.findViewById(R.id.genSetOzoneUserImage);
genSetOzoneUserImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getActivity().startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
APIAccess.fetchData(ProfileOzoneSetting.this, getActivity(), getActivity());
return rootView;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
//Toast.makeText(getActivity(), "onactivityyyyyfor result ", Toast.LENGTH_SHORT);
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
genSetOzoneUserImage.setImageURI(selectedImageUri);
setImg = true;
}
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
AppContoller.getInstance().pref=getActivity().getSharedPreferences(AppContoller.getInstance().PREF_NAME, 0);
strUserID=AppContoller.getInstance().pref.getString(AppContoller.getInstance().USER_ID, "0");
APIAccess.fetchData(ProfileOzoneSetting.this, getActivity(), getActivity());
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Please let me know what mistake I am doing here???
Chek it out. I have done little bit modification in you code. I should work.
public class ProfileOzoneSetting extends Fragment implements OnClickListener{
Boolean setImg ;
NetworkImageView genSetOzoneUserImage;
private static int RESULT_LOAD = 1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.profiesettingozone, container, false);
genSetOzoneUserImage = (NetworkImageView)rootView.findViewById(R.id.genSetOzoneUserImage);
genSetOzoneUserImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
// Show only images
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose Image"), RESULT_LOAD);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
genSetOzoneUserImage.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
catch(Exception e){
}
}
}

Image picking from Activity to Fragment

I was trying to get an image from my Album ... this code below, works perfectly on an Activity but when I use it from a fragment ... it doesn't crash, it just doesn't reach the "onActivityResult" method ... could anyone give a suggestion to whats going on
#Override
public void onClick(View v) {
if (v.getId() == mCIB.getId()) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(i, ""), RESULT_LOAD_IMAGE);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
//BitmapFactory.decodeFile(picturePath)
mCIB.setBitmapDrawable((BitmapDrawable) BitmapDrawable.createFromPath(picturePath), picturePath);
try {
mCIB.createImagePath();
mCIB.addImageToDB();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Please try this code
Parent Activity: SampleActivity.java
public class SampleActivity extends FragmentActivity {
FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parent_layout);
fragmentManager = getSupportFragmentManager();
if (savedInstanceState == null) {
fragmentManager.beginTransaction().add(R.id.container,
new SampleFragment(),"sample_fragment").commit();
}
}
Parent layout: parent.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" />
Child Fragment: SampleFragment.java
public class SampleFragment extends Fragment {
int reqCode = 111;
ImageView image;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_sample, null);
image = (ImageView) rootView.findViewById(R.id.image);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == image.getId()) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//photoPickerIntent.setType("image/*");
startActivityForResult(Intent.createChooser(photoPickerIntent, ""), reqCode);
}
}
});
return rootView;
}
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (null != imageReturnedIntent && requestCode == 111) {
//String strUriPatPic = imageReturnedIntent.getData().toString();
Uri selectedImage = imageReturnedIntent.getData();
onPictureSelected(selectedImage);
}
}
private void onPictureSelected(Uri uri) {
Bitmap userPictureBitmap = decodeSampledBitmapFromUri(getActivity(),
uri, 100, 100);
image.setImageBitmap(userPictureBitmap);
}
private Bitmap decodeSampledBitmapFromUri(Activity callingActivity,
Uri uri, int reqWidth, int reqHeight){
// TODO decode as you want
}
}

onActivityResult is not being called

I'm using Activitygroup concept to handle multiple activities under single tab. What I am doing in first activity of tab, I fetch image from gallery and camera, and after that when user selects image that image should move to second activity but those both activities must remain under the same tab. Here is my code...
TabGroupActivity.java
public class TabGroupActivity extends ActivityGroup {
private ArrayList<String> mIdList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mIdList == null) mIdList = new ArrayList<String>();
}
#Override
public void finishFromChild(Activity child) {
LocalActivityManager manager = getLocalActivityManager();
int index = mIdList.size()-1;
if (index < 1) {
finish();
return;
}
manager.destroyActivity(mIdList.get(index), true);
mIdList.remove(index); index--;
String lastId = mIdList.get(index);
Intent lastIntent = manager.getActivity(lastId).getIntent();
Window newWindow = manager.startActivity(lastId, lastIntent);
setContentView(newWindow.getDecorView());
}
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT));
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
public void onBackPressed() {
int length = mIdList.size();
if ( length > 1) {
Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
current.finish();
}
}
}
MainActivity.java
Intent intentPics = new Intent().setClass(this, GalleryPic_First.class);
TabSpec tabPics = tabHost
.newTabSpec("Photo")
.setIndicator("Photo", ressources.getDrawable(R.drawable.icon_apple_config))
.setContent(intentPics);
GalleryPic_First .java
public class GalleryPic_First extends TabGroupActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("EditActivity", new Intent(this,GalleryPic.class));
}
}
GalleryPic.java
public class GalleryPic extends Activity {
private static final int TAKE_PICTURE = 0;
private static final int REQUEST_ID = 1;
private static final int HALF = 2;
private Uri mUri;
private Bitmap mPhoto;
ImageView ivGallery, ivCamera;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_pic);
ivGallery = (ImageView)findViewById(R.id.gallery);
ivCamera = (ImageView)findViewById(R.id.snap);
ivGallery.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
getParent().startActivityForResult(intent, REQUEST_ID);
}
});
ivCamera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
File f = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
mUri = Uri.fromFile(f);
getParent().startActivityForResult(i, TAKE_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK && data != null)
{
getContentResolver().notifyChange(mUri, null);
ContentResolver cr = getContentResolver();
try
{
mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
GalleryPic_Second.bmp = mPhoto;
}
catch (Exception e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
break;
case REQUEST_ID :
InputStream stream = null;
if (resultCode == Activity.RESULT_OK && data != null) {
try
{
stream = getContentResolver().openInputStream(data.getData());
Bitmap original = BitmapFactory.decodeStream(stream);
GalleryPic_Second.bmp = Bitmap.createScaledBitmap(original, original.getWidth()/HALF, original.getHeight()/HALF, true);
Intent in = new Intent(GalleryPic.this, GalleryPic_Second.class);
TabGroupActivity parent = (TabGroupActivity)getParent();
parent.startChildActivity("ArrowsActivity", in);
}
catch (Exception e)
{
e.printStackTrace();
}
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
break;
}
}
#Override
public void onBackPressed() {
// this.getParent().onBackPressed();
TabViewLayout parentTab = (TabViewLayout) this.getParent();
parentTab.switchTabBar(1);
}
}
GalleryPic_Second.java
public class GalleryPic_Second extends Activity {
public static Bitmap bmp;
Button btnUpload;
ImageView iv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.transparent_dialog);
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.transparent_dialog, null);
this.setContentView(viewToLoad);
setResult(RESULT_OK);
iv = (ImageView)viewToLoad.findViewById(R.id.photo_holder);
btnUpload = (Button)viewToLoad.findViewById(R.id.upload);
iv.setImageBitmap(bmp);
btnUpload.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
#Override
public void onBackPressed() {
// this.getParent().onBackPressed();
finish();
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
}
}
But the image is not being displayed in second activity. How to do this?
call this way
GalleryPic.this.startActivityForResult(intent, REQUEST_ID);

Categories

Resources