Pass an Image from Activity to Adapter Class - android

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.

Related

ImageView always return blank when set programmatically

i still new to Android and want to know if there is a proper way to load image from an Input Stream.
If i load the image directly from Database the image will return the image just fine without any problem.
here my code to set ImageView Bitmap from Database :
varIDCardImageBinary = rs.getBinaryStream("IDCardImage");
classMyData.setDataInputStream(varIDCardImageBinary,"IDCardImage"); // Save Binary to a variable inside my Class
varimgIDCardView.setImageBitmap(BitmapFactory.decodeStream(classMyData.getInputStream("IDCardImage")));
Code above is working fine, And as you can see. I actually call the image AFTER i save the image into a variable.
I also put the function if the variable in my class is not empty, the ImageView Bitmap will be set from the variable without needed to call from Database again. Here is my code to call the image :
varIDCardImageBinary = classMyData.getInputStream("IDCardImage");
varimgIDCardView.setImageResource(0);
varimgIDCardView.setImageBitmap(BitmapFactory.decodeStream(varIDCardImageBinary));
And for some reason the ImageView is still BLANK, I have also tried code below to see if the Variable is empty or not :
Toast.makeText(DocumentAccountActivity.this, "Loaded From Class " + varIDCardImageBinary.toString(), Toast.LENGTH_SHORT).show();
But the result is not empty at all. Even so the ImageView still return blank. Is there anything wrong with how i set the Bitmap ? and What is the proper way to call it ?
EDITED :
I also have tried using Glide just like primo suggesting, but the result still the same.
here my code :
varIDCardImageBinary = classMyData.getInputStream("IDCardImage");
Glide.with(DocumentAccountActivity.this)
.asBitmap().load(varIDCardImageBinary).into(varimgIDCardView);
Just in case you wondering, If i call the image from Database directly. it workes just fine. But i don't want my client to load all data from database every moment they open the activity.

How to change textviews & imageviews of another activity in Android

I have an activity which can take a few seconds to load its content (mainly pictures) from the cloud. Let's say this content is pictures & description from a person. The user can go to pictures & description from another person by clicking on the next button. I'd like to avoid the loading time When this button is clicked.
To do this I have two activities : firstPersonActivity for the first person content, and secondPersonActivity for the second person content.
What I try to do is to load the content of the secondPersonActivity when the user is in the firstPersonActivity, so that the secondPersonActivity can be displayed "directly" (= without needing to load content from the cloud when the next button is clicked in the firstPersonActivity).
But I do not succeed in doing this, I don't know how to modify the views of the secondPersonActivity layout from the firstPersonActivity class.
I tested the following code in my firstPersonActivity but it doesn't work (there is no connexion to the cloud, it's just a test to better understand how it works). R.id.first_image_second_person is the id of my imageview in the secondPersonLayout (= the layout used in the secondPersonActivity).
ImageView firstImageSecondPerson = (ImageView) findViewById(R.id.first_image_second_person);
firstImageSecondPerson.setImageResource(R.drawable.mypicture);
When I click on the next button to go from the firstPersonActivity to the secondPersonActivity, my firstImageSecondPerson imageview is not filled.
Do you see what's wrong ?
Is there a better way to avoid the loading time when the user click on the next button ?
It is not possible to change activity if activity instance is not created. In my opinion to do what You need I would go to single activity with hidden content of second person ( VIEW.INVISIBLE ) and show/hide it when it is needed.
But if second activity must be there, then create some structure for saving bitmaps in memory. In Your code sample You get picture from drawable so we are not talking about some delays, but if images are downloaded from some server then You can create some class which will have those bitmaps created from url and instance of this class can be used on any activity.
So for example Your class for caching pictures would have some method for creating bitmaps like -How to load an ImageView by URL in Android?. And those bitmaps should be saved in some Array or HashMap to have access to it, for example ( pseudo code ):
//some structure for cashing pictures in app
class PictureCache {
static PictureCache instance;
final HashMap<Integer, Bitmap> bitmaps;
//singleton
static PictureCache getInstance(){
if (instance==null)
instance = new PictureCache();
return instance;
}
public PictureCache(){
bitmaps = new HashMap<>;
}
private Bitmap loadBitmap(String url);//#1
public addPicture(Integer personId, String href){
//add to hashMap
bitmaps.put(personId, loadBitmap(href));
}
public Bitmap getPicture(Integer personId){
return bitmaps.get(personId);
}
}
#1 - method from How to load an ImageView by URL in Android?
Using it in first activity:
PictureCache.getInstance().addPicture(12,"http://url.to.bitmap");
Using it in second activity:
Bitmap pic = PictureCache.getInstance().getPicture(12);
Important note - above code was written here and was not tested, it shows solution concept.
Important second note - using such approach with bitmaps in memory can cause to much memory usage
You cannot access the views of SecondActivity before its creation. If you called this activity once only then you are able to access its views by making them static.
One more Solution for this is..
Access the whole data at once and save it in static arraylist with the help of getter-setters. Then on SecondActivity set data from that arraylist.
Hope this will work.
I don't think you can access the view before creating the activity.
You can try to use Glide for caching your images and minimizing loading time.
Try this
Picasso.with(getContext()).load(R.drawable.generatedId).into(imageView);
or
imageView.setImageDrawable(ActivityCompat.getDrawable(getContext(),
R.drawable.generatedID));`

Pass image from Listview to a detail activity

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/

How to send Listview item data (image & text) from one activity to another activity in android

I am working on application in which user will be able to retrieve data from weburl using json and i have made that module, but now i want to show selected listview item in another activity using intent in which i have to show them Image and Text of selected Item...could you please show me some source code or way to write the code for the same one.
If you're inflating a view from JSON data, couldn't you just send the JSON string as an extra with the activity intent, and the inflate the view inside the second activity?
For example,
Intent.putExtra("json",mJson);
where mJson is the name of your string object containing the JSON. Then in your other activity's onResume, do something like
mJson = getIntent().getExtras().getString("json");
And then inflate the view using that JSON string.
You can not simply add image into your bundle.you can add your text simply like
Intent.putExtra("text",yourText);
To add image into bundle refer this,
how do you pass images (bitmaps) between android activities using bundles?
You have to use parceble object within bundle.

How pass an image resource from my app to another?

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);

Categories

Resources