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...
Related
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;
}
}
This question already has answers here:
Capture Image from Camera and Display in Activity
(19 answers)
Closed 5 years ago.
I am trying to display a image captured from camera inside imageview in android
Selecting a photo from the correct gallery works
But the camera fails: "app has stopped"
images.get(2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
current_image = 2;
if (!fill_images[current_image]) {
show_dialog();
}else {
show_delete_dialog();
};
}
});
public void show_dialog() {
final ArrayList<String> list = new ArrayList<String>();
list.add("select from gallery");
list.add("take a photo");
AlertDialog.Builder builder = new AlertDialog.Builder(NewAdActivity.this);
builder.setAdapter(new ArrayAdapter<String>(NewAdActivity.this, R.layout.row, R.id.mytext, list)
, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i == 0) {//gallery Intent gallery_intent=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ; startActivityForResult(Intent.createChooser(gallery_intent,"select image"),2);
}
else if (i == 1){//camera
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file=new File(Environment.getExternalStorageDirectory(),"file"+String.valueOf(System.currentTimeMillis()+".jpg"));
uri= Uri.fromFile(file);
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
camera_intent.putExtra("return-data",true);
startActivityForResult(camera_intent,1);
}
}
});
builder.show();
}
At first make sure that you declared all necessary permission on manifest.xml file. Then try with it -
public class MainActivity extends AppCompatActivity {
private static final int SELECTED_PICTURE=1;
private ImageView iv;
private Button btnClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
iv=(ImageView)findViewById(R.id.imageView1);
btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(new View.OnCLickListener(){
public void onClick(View view) {
btnClick(view);
}
});
}
public void btnClick(View v){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, SELECTED_PICTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(photo);
}
}
}
And dont forget to add permission to manifest.xml -
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
new Camera.PictureCallback() {
#Override
public void onPictureTaken(final byte[] data, Camera camera) {
bytes = data;
}
}
Then convert the bytes to bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Then set imageview
imageView.setImageBitmap(bitmap);
I tried using a "speech to text" feature in my app.
it opens up the recording window/ dialog or whatever but the text doesn't show.
this is the code:
public void onTalk(View v){
promptSpeechInput();
}
public void promptSpeechInput()
{
Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT,"say something");
try{
startActivityForResult(i, 50);
}
catch(ActivityNotFoundException e){
Toast.makeText(this,"sorry your device doesnt support speech language",Toast.LENGTH_LONG).show();
}
}
then there's the onResult part:
public void onActivityResult(int request_code, int result_code, Intent i){
super.onActivityResult(request_code,result_code,i);
if (request_code==RESULT_OK)
if(i!=null) {
EditText locationTS = (EditText) findViewById (R.id.locationET);
ArrayList<String> result = i.getStringArrayListExtra((RecognizerIntent.EXTRA_RESULTS));
locationTS.setText(result.get(0));
}
}
locationTS is an EditText, and it does not show any text after i talk.
This worked in my case:
I had a separate Helper class as:
public class SpeechRecognitionHelper {
private static final int VOICE_RECOGNITION_REQUEST_CODE=101;
public static void run(Activity activity){
if(isSpeechRecognitionActivityPresent(activity)){
startRecognition(activity);
}
else {
Toast.makeText(activity,"You must install Google Voice Search",Toast.LENGTH_LONG).show();
installGoogleVoiceSearch(activity);
}
}
private static boolean isSpeechRecognitionActivityPresent(Activity activity){
try {
PackageManager packageManager = activity.getPackageManager();
List activities = packageManager.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);
if (activities.size()!=0){
return true;
}
}
catch (Exception e){
}
return false;
}
public static void startRecognition(Activity activity){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Say Something");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
activity.startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);
}
private static void installGoogleVoiceSearch(final Activity activity){
Dialog dialog = new AlertDialog.Builder(activity)
.setMessage("For recognition Install Google Voice Search")
.setTitle("Install voice search?")
.setPositiveButton("Install", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.voicesearch"));
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
activity.startActivity(intent);
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
}
}
Then in the MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final int VOICE_RECOGNITION_REQUEST_CODE=101;
private static final int CAMERA_REQUEST=201;
private Button buttonVoiceCommand;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonVoiceCommand = (Button)findViewById(R.id.buttonVoiceCommand);
buttonVoiceCommand.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.buttonVoiceCommand){
run(MainActivity.this);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(matches.size()>0){
if(matches.get(0).indexOf("camera")>0){
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAMERA_REQUEST);
}
else {
Toast.makeText(this,matches.get(0),Toast.LENGTH_LONG).show();
}
}
}
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
//Perform whatever you want to do (-.-)
Toast.makeText(this,"Camera Working",Toast.LENGTH_LONG).show();
}
}
}
I Toast the content of matches.get(0) You just have to set it in an edit text
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){
}
}
}
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);