I have 2 apps running, and I need to pass an image resource from the first app to the second.
The ImageView have a setImageURI(Uri) method, that I could use in the second app, but does not have a getUri() for me to use in the first.
Any idea how to do this?
-- update
looks like Content Providers can solve the problem. (studying)
The only way is to pass the data to the second Activity when you start it. If you check out the Intent API you can pass the Uri using one of the putExtra() methods, and in the onCreate for the new Activity you can retrieve the Uri using getStringExtra().
You can pass a (Bitmap)Drawable this way:
// sending side
BitmapDrawable bd = (BitmapDrawable)imageView.getDrawable();
intent.putExtra("img", bd);
// receiving side
Bitmap b = (Bitmap) intent.getParcelable("img");
imageView.setImageBitmap(b);
Related
I want to pass about 10 images form main activity to other activty. which method is better?
converting to bitmap and then pass the pictures,get image id and pass id, using image URL in drawable folder or maybe there is are way.
can passing image in bitmap form slow the running of app?
If the images can be found in the drawable folder, better pass the resource ids of those drawables instead. If the images are being downloaded from the internet (e.g. asynctask) then you pass the bitmaps to another activity. Hope this helps.
If the image is in drawable folder best way is to pass the id.
Example:
Intent i = new Intent(this, DestinationActivity.class);
Bundle b = new Bundle();
b.putInt("FirstImage", R.drawable.nameofyourimage);
i.putExtras(b);
startActivity(i);
and get it in the second activity with getIntent().getExtras()
If the image is on a server, pass the url of the image and download it in the second activity.
In my app I have a profile pic of the user. If he previously logged in, I download it from my server, otherwise he takes a pic from the camera or gallery. Either way, I store it in a class called DataStorage in a static public Bitmap variable thus:
public class DataStorage {
.
.
public static Bitmap origProfilePic; //not saved on app close
.
.
Now, if the user decides to re-do his profile, I log him out send him back to the profile creation screen, and I call:
DataStorage.origProfilePic = null;
(By the way, null is a legitimate value for this picture - we allow users to have no picture). The profile creation is through 2 activities - first choosing a username and password, then choosing a picture and other details. What's weird is that although I set the picture variable to null, somehow when I get to the end of the profile creation, it has gone back to it's original value, and I somehow have a user with the old profile picture (this is in the case where he did not select a new picture)
I've logged it n the OnCreate functions of the two activities:
onCreate SignUpActivity1, origPic = null
then
onCreate SignUpActivity2, origPic = android.graphics.Bitmap#42ba7438
But SignupActivity1 calls the intent for SignUpActivity2 and nowhere in SiugnupActivity1 is there a reference to DataStorage.origProfilePic except in my log statements.
I decided to check one more time in SignUpActivity1 if the value changes and I found...just as I invoke SignupActivity2:
Intent intent = new Intent(SignUpActivity1.this, SignUpActivity2.class);
intent.putExtra("EMAIL", email.toString());
intent.putExtra("PASSWORD", password.toString());
startActivity(intent);
Log.e("Kibi", "End SignUpActivity1, origPic = " + DataStorage.origProfilePic);
finish();
This somehow has:
End SignUpActivity1, origPic = android.graphics.Bitmap#42ba7438
This is quite repeatable in this specific case, though I certainly set the bitmap to null in other places and don't see it popping back up.
I saw a few things on SO about static Bitmaps being a danger for memory leaks, though I don't see a memory leak right now, I worry that I must be doing something very wrong if setting to null cannot be relied upon.
Can anyone explain what I am doing wrong and what the best way is to store this Bitmap in a basically volatile way?
It makes sense that both values change when you use DataStorage.origProfilePic in both activities. Since it's a static value, both activities are pointing to the same memory object.
DataStorage.origProfilePic is pointing to a piece of memory where your bitmap is stored. Assuming you use something like:
#SignUpActivity1
Bitmap origPic = DataStorage.origProfilePic;
#SignUpActivity2
Bitmap origPic = DataStorage.origProfilePic;
What happens here is that you create a reference to DataStorage.origProfilePic. That means you practically have the same value in SignUpActivity1.origPic and SignUpActivity2.origPic.
When you call this rule:
startActivity(intent);
The OnCreate method in SignUpActivity2 will be invoked. You probably will set here (or somewhere else) the value of DataStorage.origProfilePic, which means the value of SignUpActivity1.origPic and SignUpActivity2.origPic also changed (not entire true, but will have a different output).
Also be carefull with DataStorage.origProfilePic = null;. This only set the reference to null, but doesn't clear the bitmap from memory. Use DataStorage.origProfilePic.recycle(); to clear the bitmap from memory.
I have a listview with text and images from web with JSON (works fine) and when I click on that I want to go to a DetailActivity with some TextViews and the ImageView from list. I can pass all the Textviews from the ListActivity to the DetailActivity but not the ImageView.
I tried to pass the bitmap with putextras and model but nothing. How can approach that issue? Can I call the bitmap from cache direct to the DetailActivity?
You can use ImageLoader
Intent i = getIntent();
// Get the result of flag
flag = i.getStringExtra("flag"); // image
// Locate the TextViews and images in singleitemview.xml
ImageView imgflag = (ImageView) findViewById(R.id.flag);
imageLoader.DisplayImage(flag, imgflag); // use lazylist image loader
You can see this example.
Did you save the image in the local storage? In this case, it would be better to send the Uri of the saved file instead of the raw bitmap.
Passing large amounts of data by intent does not always work. Although not documented, this often fails. See
Issue: Passing large data to second Activity
Starting an activity with large intent extras
and many more.
A good work-around would be to (a) save the bitmap to a file (b) pass the filename, by intent, to the detail activity, (c) In the detail activity re-create the bitmap.
The easiest way to pass complex objects that I have found is using EventBus: https://github.com/greenrobot/EventBus
Using EventBus you avoid having to serialize the data in any way, they are made directly available for other Activities/Fragments and regular classes even.
My answer to this question has some explanation on the subject: Saving information from one fragment and dialog if the user navigates to another fragment
This article also has some nice small examples of how simple it is plus some comparison to other approaches: http://www.stevenmarkford.com/passing-objects-between-android-activities/
I am trying to pass an Image from SingleItem activity to CartAdapter, but i am facing problem, while trying to pass an Item Image.
I want whenever user do click on Add to Cart button in SingleItem activity, need to send Item Image and Strings from SingleItem.java to CartAdapter.java
Note: Able to pass String but trying to pass an Item Image, I am missing some code onButtonClick in SingleItem.java
Like using below line i am getting value for Item Title
myTitle = txttitle.getText().toString();
Log.d(SingleItem.LOG_TAG, "Title :: " + myTitle);
but i don't know how to get an Item Image
myThumb = // here what i should need to write
See my code below:
SingleItem.java::
// below i need your help
myThumb = // here i want to write code to get Image,
// like above i have written code to get String
Instead of sending Bitmap, write the Bitmap to file system and send its location so that the other activity can read the Bitmap from that location.
Sending large data through intents could cause ANRs
Bitmap Class implements Parcelable so you can pass your Bitmap from one class to another class but i will not recommend this. In my experience in case of large Bitmaps, this could cause ANR in android, you can use Singleton Pattern for Interclass communication for heavy objects, if you are using Singleton make sure you read multithreading issues.
Use Hash Map. In hash map put bitmap with key and pass this hash map to the Adapter. And Get that hash map in adapter class.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I pass data between activities in Android?
I have been banging my head across the table for days trying to figure this out. I'm new to both Java and the Android platform and I am writing my second application. In a nutshell, I want the user to enter in some information in the first activity. That activity then takes those inputs and runs a series of mathematical computations and then displays the results in several editTexts in a separate activity. In my mind, the application takes the input, runs the computations and then stores them in some variables. The variables then need to be passed to the second activity which then displays the results. I have most of the application coded and I can get it to work properly if I keep the inputs and displayed results in one activity. Any Ideas on how I can get the displayed results to show up on another activity? Any direction here is much appreciated.
This can be done with use of intent. one of the use of Intent is to pass the data between activities. In your scenario what you need to do is
STEP 1
After taking input from the user, do computation, store result in
the variables
bundle that in the intent which you are using to
start next activity. You can achieve this by below code
Intent intent = new Intent();
intent.putExtra("KeyForResult1", <your variable>);
intent.putExtra("KeyForResult2",<your variable>);
startActivity(this, nextactivity.class);
in the nextactivity you need to get the intent and extract the values in the variable
which can be achieved
variabletype variable = getIntent().getExtras().get("KeyForResult1");
variabletype variable = getIntent().getExtras().get("KeyForResult2");
You can use this:
ActivityOne.class:
//compute the data and get the result here
//suppose results are,
int resultInt=24;
String resultString="abc";
Intent intent=new Intent(getApplicationContext(),ActivityTwo.class);
intent.putExtra("ResultInInteger",resultInt);
intent.putExtra("ResultInString",resultString);
startActivity(intent);
this will open ActivityTwo.class,where you can get the data like:
int resultInt=getIntent().getIntExtra("ResultInInteger");
String resultString=getIntent().getStringExtra("ResultInString");
I was able to figure it out with a mixture of data I had researched. It seems what finally worked was
int resultInt=24;
Intent myIntent = new Intent(CalPalActivity.this, ResultsActivity.class);
myIntent.putExtra("intVariableName", resultInt);
startActivity(myIntent);
in the first activity and
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
in the second. Why it worked like this and kept giving me errors such as "the method getIntExtra(String, int) in the type Intent is not applicable for the arguments (String)" in the second activity I'll never know. Even the Quick-fixes didn't fix it (I just kept getting new errors). All in all, it's an expensive (5 hour +) but well learned lesson. All of your help was much appreciated. I was able to understand and learn a great deal more about the language. Thank you!
You should read up on Intents. You can store extras in an intent, much like URL parameters. If you use that intent to start your next activity, it will be able to extract the data from the intent's extras.
There are several options. For your task I'd choose passing the data in Intent's extras.
For all options read this
Here is an option.
In first Activity:
Intent yourNextActivityIn=new Intent(this,yourNextActivity.class);
yourNextActivityIn.putExtra("tag",valueToBePassed);
startActivity(yourNextActivityIn);
In second Activity:
//If value is an Integer with default value 1
int value = getIntent().getIntExtra("tag", 1);
else
//if value is a string
String value = getIntent().getStringExtra("tag");