I am having a imageView in one class and on clicking the imageView a dialog box appear which has two option to take a image from camera or open the image gallery of device. I want to send image from one class to another so it can appear in ImageView. I am searching from many hour but i got only about sending text data from one class to another.Can any one tell about sending an image from one class to another?
This is code from sender class which will take image.
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
bmp=(Bitmap)extras.get("data");
}
}
For any help thanks
You get Image in your Activity as a Bitmap and you also pass that to another Activity as Bitmap with Intent.putExtra() like this:
First Activity.
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bmp_Image", bmp);
and get from second Activity like:
Bitmap bmp = (Bitmap) intent.getParcelableExtra("bmp_Image");
you don't need to get url and load from url.
that is the simplest way to pass the captured image from one Activity to another Activity.
I remember something about that there is a limitation in size for putExtra() and getExtra() about 1mb. So a picture may exceed this limitation.
How about just passing the path to the picture?
My preferred way (and I think the most straight-forward way) is to use an own Application instance in the app, to store variables that are common to more than 1 activity.
Create a class, let's call it MainApplication that extends android.app.Application
and declare it in the manifest:
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:name=".MainApplication">
Then you get an instance of the application object in the Activity like this:
MainApplication application = ((MainApplication)getApplication());
Inside this application object you can store any app-level data and use it as usual:
application.setImage(...);
application.getImage();
I got the answer you need to send path of image from one activity to another.
filePath is the path of image.
Intent open_displayPage=new Intent(MainActivity.this,display_page.class);
open_displayPage.putExtra("imagePath", filePath);
And get the path in another activity
final String path = getIntent().getStringExtra("imagePath");
org_bmp = BitmapFactory.decodeFile(path);
Take One Global.class and Declare public static Bitmap bmp;
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
Global.bmp=(Bitmap)extras.get("data");
}
}
And When u want to use Bitmap bitmap = Global.bmp ;
I'm going to show you the best way okay.
1st) Get and send the image URI
Uri imageUri = data.getData();
Intent newIntent = new Intent(Class.this, Class.class);
newIntent.putExtra(IMAGE_URI_KEY, imageUri);
startActivity(newIntent);
2nd) Receive the image and how to show it
receivedImageUri = getIntent().getParcelableExtra(IMAGE_URI_KEY);
imageView.setImageURI(receivedImageUri);
I had to rescale the bitmap a bit to not exceed the 1mb limit of the transaction binder. You can adapt the 400 the your screen or make it dinamic it's just meant to be an example. It works fine and the quality is nice. Its also a lot faster then saving the image and loading it after but you have the size limitation.
public void loadNextActivity(){
Intent confirmBMP = new Intent(this,ConfirmBMPActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = returnScaledBMP();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
confirmBMP.putExtra("Bitmap",bmp);
startActivity(confirmBMP);
finish();
}
public Bitmap returnScaledBMP(){
Bitmap bmp=null;
bmp = tempBitmap;
bmp = createScaledBitmapKeepingAspectRatio(bmp,400);
return bmp;
}
After you recover the bmp in your nextActivity with the following code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmBMP);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
}
I hope my answer was somehow helpfull. Greetings
You could use a Singleton Object to store your Image:
public class SingletonModel {
private Bitmap Image;
private SingletonModel;
public static SingletonModel getInstance() {
if (instance == null) {
instance = new SingletonModel();
}
return instance;
}
public Bitmap getImage() {
return this.Image
}
public Bitmap setImage(Bitmap ImageIn) {
this.Image = ImageIn;
}
}
And in your first Activity put:
SingletonModel.getInstance().setImage(image);
And in your second Activity:
Bitmap image = SingletonModel.getInstance().getImage();
In alternative you could create an Object which extend Application, so this Object is visible for all class (the idea is the same to a Singleton Object).
Related
I am having a imageView in one class and on clicking the imageView a dialog box appear which has two option to take a image from camera or open the image gallery of device. I want to send image from one class to another so it can appear in ImageView. I am searching from many hour but i got only about sending text data from one class to another.Can any one tell about sending an image from one class to another?
This is code from sender class which will take image.
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
bmp=(Bitmap)extras.get("data");
}
}
For any help thanks
You get Image in your Activity as a Bitmap and you also pass that to another Activity as Bitmap with Intent.putExtra() like this:
First Activity.
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bmp_Image", bmp);
and get from second Activity like:
Bitmap bmp = (Bitmap) intent.getParcelableExtra("bmp_Image");
you don't need to get url and load from url.
that is the simplest way to pass the captured image from one Activity to another Activity.
I remember something about that there is a limitation in size for putExtra() and getExtra() about 1mb. So a picture may exceed this limitation.
How about just passing the path to the picture?
My preferred way (and I think the most straight-forward way) is to use an own Application instance in the app, to store variables that are common to more than 1 activity.
Create a class, let's call it MainApplication that extends android.app.Application
and declare it in the manifest:
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:name=".MainApplication">
Then you get an instance of the application object in the Activity like this:
MainApplication application = ((MainApplication)getApplication());
Inside this application object you can store any app-level data and use it as usual:
application.setImage(...);
application.getImage();
I got the answer you need to send path of image from one activity to another.
filePath is the path of image.
Intent open_displayPage=new Intent(MainActivity.this,display_page.class);
open_displayPage.putExtra("imagePath", filePath);
And get the path in another activity
final String path = getIntent().getStringExtra("imagePath");
org_bmp = BitmapFactory.decodeFile(path);
Take One Global.class and Declare public static Bitmap bmp;
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
Global.bmp=(Bitmap)extras.get("data");
}
}
And When u want to use Bitmap bitmap = Global.bmp ;
I'm going to show you the best way okay.
1st) Get and send the image URI
Uri imageUri = data.getData();
Intent newIntent = new Intent(Class.this, Class.class);
newIntent.putExtra(IMAGE_URI_KEY, imageUri);
startActivity(newIntent);
2nd) Receive the image and how to show it
receivedImageUri = getIntent().getParcelableExtra(IMAGE_URI_KEY);
imageView.setImageURI(receivedImageUri);
I had to rescale the bitmap a bit to not exceed the 1mb limit of the transaction binder. You can adapt the 400 the your screen or make it dinamic it's just meant to be an example. It works fine and the quality is nice. Its also a lot faster then saving the image and loading it after but you have the size limitation.
public void loadNextActivity(){
Intent confirmBMP = new Intent(this,ConfirmBMPActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = returnScaledBMP();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
confirmBMP.putExtra("Bitmap",bmp);
startActivity(confirmBMP);
finish();
}
public Bitmap returnScaledBMP(){
Bitmap bmp=null;
bmp = tempBitmap;
bmp = createScaledBitmapKeepingAspectRatio(bmp,400);
return bmp;
}
After you recover the bmp in your nextActivity with the following code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmBMP);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
}
I hope my answer was somehow helpfull. Greetings
You could use a Singleton Object to store your Image:
public class SingletonModel {
private Bitmap Image;
private SingletonModel;
public static SingletonModel getInstance() {
if (instance == null) {
instance = new SingletonModel();
}
return instance;
}
public Bitmap getImage() {
return this.Image
}
public Bitmap setImage(Bitmap ImageIn) {
this.Image = ImageIn;
}
}
And in your first Activity put:
SingletonModel.getInstance().setImage(image);
And in your second Activity:
Bitmap image = SingletonModel.getInstance().getImage();
In alternative you could create an Object which extend Application, so this Object is visible for all class (the idea is the same to a Singleton Object).
I tried to convert a TextView into Bitmap and I tried to pass it to another Activity on a single Button click.
My code is:
Activity sending the Bitmap :
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view0 = txt1.getRootView(); //txt1 is a TextView
view0.setDrawingCacheEnabled(true);
view0.buildDrawingCache();
Bitmap bmp0 = Bitmap.createBitmap(view0.getDrawingCache());
Intent in = new Intent(Meme_make.this,S_meme.class);
in.putExtra("bm0" , bmp0);
startActivity(in);
}
Activity Receiving the Bitmap :
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sm);
main1 = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = getIntent().getParcelableExtra("bm0");
main1.setImageBitmap(bitmap);
}
But when I click on the button , the activity is automatically closing and taking me back to the Main launcher Activity.
How to do this without any issues ?
You have
in.putExtra("bm0" , bmp0);
and
Bitmap bitmap = getIntent().getParcelableExtra("bmp0");
In particular bm0 vs bmp0, you may just be missing a p.
But in general I would also cast your retrieved bitmap to a bitmap
Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("bmp0");
Try this code snippet.
Send bitmap
Intent intent = new Intent(this, ActivityName.class);
intent.putExtra("Bitmap", bitmap);
Retrieve Bitmap
Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("Bitmap");
I have a method that needs to be called from another activity, and I need to use it to set an ImageView in it's own activity. I have this method in MainActivity:
public static void setImageView(String fileName){
Log.i(TAG, fileName);
imageView = (ImageView) findViewById(R.id.imageView);
bmp = BitmapFactory.decodeFile(fileName);
imageView.setBackgroundResource(0);
imageView.setImageBitmap(bmp);
}
But I can't make a static reference to findViewById because it isn't a static method. This method is being called in a Camera Activity after the photo has been saved, I want to pass in the fileName (file URI) and set the imageView such that when the Camera Activity finishes and the user return to the MainActivity the ImageView is already set. As such, in CameraView I am trying to call this:
...code...
mCamera.takePicture(null, null, callback);
MainActivity.setImageView(fileName);
Is there a cheeky way around this? I know there are other posts on this but I can't quite work out how to apply the advice given there to my situation.
Thanks!
Maybe you should launch your CameraActivity using startActivityForResult() method, and after take the photo put the fileName as an Extra into an Intent and set it as result. Then in your MainActivity you can get the fileName back from the Intent arg of onActivityResult().
Something like:
public class MainActivity extends Activity{
public void aMethod(){
...
Intent i = new Intent(this, CameraActivity.class);
startActivityForResult(i, REQUEST_CODE);
...
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Bundle b = data.getExtras();
String fileName = b.getString(RETURN_FILE_PARAMETER);
doSomething(fileName);
}
}
}
...
public class CameraActivity extends Activity{
private void returnFileFinishActivity(String fileName) {
Intent retIntent = new Intent();
retIntent.putExtra(RETURN_FILE_PARAMETER, fileName);
setResult(RESULT_OK, retIntent);
finish();
}
}
Regards.
A few things.
1) Since you should only be displaying one activity at a time, why not just start with startActivityForResult in the activity with the ImageView and override onActivityResult in your camera activity?
2) I'm not sure of your application, but it may be easier if you implement taking a picture by following this: http://mobile.tutsplus.com/tutorials/android/android-sdk-quick-tip-launching-the-camera/ .
3) You can expose a static reference to your main activity, in your main activity's onCreate method, do something like staticRef = this; and in your camera activity simply access it via MainActivity.staticRef... (I would not recommend this approach)
4) You can register a broadcast receiver in your Main activity that has a reference to your main activity or image view and in your camera activity you send a broadcast to it which you can set the image view
I'm trying to take apicture and I want to pass this picture to another activity to be displayed in it.
OnClickListener listener_TakePic = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
Bitmap image = (Bitmap) data.getExtras().get("data");
}// End IF
}// End of Method
Please tell me what should I do or modify to achieve what I want..
Actually, i have created an application class "base class" in which, I have defined an "bitmap cameraPic" and "static final int CAMERA_PIC_REQUEST = 1;"
Now, I have an activity classed named "AddLocation" from which I call startActivityForResult(intent, CAMERA_PIC_REQUEST);
but I want the result of the camera Picture to appear in another activity called "MPActivity"
My question is, should I call "onActivityResult(int requestCode, int resultCode, Intent data)" method from "MPActivity" or there is no need to call it...
i'm really confused please clarify..
There are more than one way to tackle the task you are doing.
you can build a java class and put populates its one of property by the desired image. Then you can just get stuff that into an arraylist and pass that to a global arraylist.
you can use Application class and make a global image variable that can be accessed in every class in your activity.
etc ...
my onActivityResult method is never called. am using android 2.2
I am using a Tabhost, where TabHosts contain TabGroups which contain individual Activities.
One of my individual activity runs the following intent
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), 0);
this loads my gallery apps, I use the default android gallery to select one image and when I return my onActivityResult is not called my activity.
It looks like this - and I put a breakpoint at if(resultCode == 0) , so right now, the logic of my onActivityResult should not matter
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 0) {
if (requestCode == 0) {
Uri selectedImageUri = data.getData();
//OI FILE Manager
filemanagerstring = selectedImageUri.getPath();
//MEDIA GALLERY
selectedImagePath = getPath(selectedImageUri);
//DEBUG PURPOSE - you can delete this if you want
if(selectedImagePath!=null)
System.out.println(selectedImagePath);
else System.out.println("selectedImagePath is null");
if(filemanagerstring!=null)
System.out.println(filemanagerstring);
else System.out.println("filemanagerstring is null");
//NOW WE HAVE OUR WANTED STRING
if(selectedImagePath!=null)
System.out.println("selectedImagePath is the right one for you!");
else
System.out.println("filemanagerstring is the right one for you!");
}
}
}
Lifecycle functions are often called out of order and intermittently for Activities within a tabhost/tabgroup, so I checked to see what lifecycle functions ARE being called after the gallery closes (this happens as soon as I select an image from the android gallery)
The only one being called is the onResume() in my TabHost activity. So I tried putting the exact same onActivityResult() method in my TabHost class AS WELL AS the TabActivity class. With a breakpoint in the same location at the beginning of method.
Neither of these classes are called.
I'm drawing a blank now, how can I get the result from the gallery app in my app if none of the built in receiving methods will respond to it.
Since I know that my main TabHost gets the onResume() called, I tried added Intent graphics = getIntent(); to see if it would receive data from the gallery selection, it does not, so I don't see how I can do the logic in the onResume() method either.
Solutions welcome! :)
Try to call the startActivityForResult using the context of the tabgroup activity containing your current activity and then listen in the tabgroup activity.
Use this to get the tabGroupActivity:
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
And then call startActivityForResult from it:
parentActivity.startActivityForResult(...);
Finally , put an onActivityResult listener in the tabGroupActivity:
protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
}
Judging from the many questions like this one, there are many reasons why a called activity may not trigger the caller's onActivityResult() method.
One reason I found, was when I called startActivityForResult(intent, requestCode), with a requestCode value of less than 0. My application did not need a requestCode and the Android documentation said using < 0 would not send a requestCode.
But the Android docs did not mention the consequence of a requestCode < 0. The consequence is that it prevents the caller's onActivityResult() method from ever being invoked! Ouch!
Therefore, even if your app does not need a requestCode, you many still want to use one with a value >= 0.
That's what I learned today:-)
The solution is to call a transparent activity over top of the main activity. This transparent activity is in front of the tabhost and will have normal lifecycle functions.
This transparent activity calls the gallery intent onCreate(), it gets everything returned like normal in its onActivityResult and you will be able to pass the information returned back to the rest of the app like normal. finish() is inside of the onActivityResult method, so the user never even notices that a transparent activity was called.
Update copied from from comments:
Activity A calls Activity B via normal intent. Activity B has no xml and runs onCreate like this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.dialogpopper);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*"); startActivityForResult(intent, 0);
}//end onCreate
and when Activity C is finished it calls the onActivityResult of Activity B
You just have to remove android:noHistory="true" this form your manifest file.
Use the constant values for the Result codes:
Activity.RESULT_OK and
Activity.RESULT_CANCELED
You'll see that the value for cancelled is actually 0. So in your code you are checking to see if the activity was cancelled.
change your code to
if (resultCode == Activity.RESULT_OK) {
...
}
Additionally change your Intent action to be:
intent.setAction(Intent.ACTION_PICK);
If you do this, you can just call
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 0);
instead of creating the chooser. It will automatically pick the activities associated with that intent and mimetype and display them to you
The way onActivityResult is called depends on the launchMode of your Activity (in the manifest). I'm not sure if that can be an issue here.
do you have #Override above your onActivityRestult?
(looking at old code that does this so not sure why its needed) call super.onactivityresult(requestcode, resultscode, data) as the first call in the method
also my intents didnt have that other stuff in them
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), 0);
i think should just be
startActivityForResult(source.class, destination.class);
of course source and destination should be the name of the classes
public class ImageSwitcherView extends Activity {
int pics[] = { R.drawable.image000, R.drawable.image001,
R.drawable.image002};
private int currentIndex = 0;
SharedPreferences preferences;
Gallery gallery;
ImageView fullPicView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galleryview);
Bundle extras = getIntent().getExtras();
currentIndex = extras.getInt("bookmark");
gallery = (Gallery) findViewById(R.id.Gallery01);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSelection(currentIndex);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
currentIndex = position;
// ---display the images selected---
fullPicView = (ImageView) findViewById(R.id.imageView1);
fullPicView.setImageResource(pics[currentIndex]);
fullPicView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(ImageSwitcherView.this,
imageView.class);
int resID = pics[currentIndex];
myIntent.putExtra("resID", resID);
myIntent.putExtra("index", currentIndex);
startActivityForResult(myIntent, 1);
}
});
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c) {
context = c;
// ---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
// ---returns the number of images---
public int getCount() {
return pics.length;
}
// ---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(pics[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
currentIndex = data.getIntExtra("bookmark", 0);
gallery.setSelection(currentIndex);
fullPicView.setImageResource(pics[currentIndex]);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
|| keyCode == KeyEvent.KEYCODE_HOME) {
preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("bookmark", gallery.getSelectedItemPosition());
editor.commit();
finish();
}
return super.onKeyDown(keyCode, event);
}
}