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.
Related
Hi I'm implementing click listeners in the following way but after some time the methods and variables inside the listener's closure get the wrong values or something. Let me explain the implementation of the listener a little better a for loop creates the listener for a set of image views then later in the program the for loop is called a second time and it resets the listener methods and variables to different values. Everything works great for about 30 minutes but then for some reason, the listener's methods and variables start having the wrong values. Has anybody ever heard of this behavior or can tell me where I've gone wrong with the code? Keep in mind that the listener I'm about to paste here is just a small piece of a 1014 line class. I'm hoping somebody can spot How I'm implementing the listener wrongly and can give me some advice on how to "reset" the listener so that it's variables and values stay over time. Hopefully you can read the code without putting it in an editor but feel free to copy it for readability's sake Here is the code for the image view listener with comments.
//image views are held in an array
//set an image view in its imageview container
imgArr0[slotId1].invalidate()
imgArr0[slotId1].setImageDrawable(null)
//drw is not defined in this example
imgArr0[slotId1].setImageDrawable(drw)
/*if video or image id is set to image then set a listener for the image
*/
/*slotId1 is not defined in this example but it is simply a counter to iterate over the ImageView array
*/
if (videoOrImageId0[slotId1] == "image") {
//null any listeners that might be attached to the image view
imgArr0[slotId1].setOnClickListener(null)
//set or reset the listener
imgArr0[slotId1].setOnClickListener() {
`enter code here`//if the current config is portrait then set a new image image
if (currentConfig0 == "portrait0") {
act0.lrgImage0.invalidate()
act0.lrgImage0.setImageDrawable(null)
/*drw is not defined in this example but works fine in the production script
*/
act0.lrgImage0.setImageDrawable(drw)
}
--calmchess
ccc tv application with problem.
(https://i.stack.imgur.com/PjdbN.jpg)![enter image description here](https://i.stack.imgur.com/FaMnc.
I was able to partially solve this question by destroying all the image views and their associated click listeners then rebuilding those... However I don't consider this issue completely solved so if anybody can provide a better solution I'd love to hear it because rebuilding the images every few minutes has to be using a lot of unnecessary hardware resources.
--calmchess
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));`
I wanted to know if there is a way to make the BindView work only once per item?
Are items that we scroll away from the screen getting destroyed? and when they come back we must run bindview again?
The reason is I got my adapter to Download an image and set it on the item.
when i scroll the image is getting downloaded again, even if i didnt quit the application.
the bindview is called each time an item "returns" into display so even tho i already downloaded it, it will preform the asyntask again ( the asyntask is called in the adapter)
I even tried saving BLOB on my database after each download, to know when to call the asyntast and when just to make a bitmap out of the array
if(photoBArray != null){
Bitmap bitmap = BitmapFactory.decodeByteArray(photoBArray, 0, photoBArray.length);
holder.icon.setImageBitmap(bitmap);
}else{
holder.icon.setImageResource(R.drawable.loader);
String urlString = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=100&photoreference="
+ c.getString(c.getColumnIndex(PlacesDBHelper.PHOTO_REFERENCE_COL))+"&key=API_KEY";
Log.e("Photo REFERENCE", c.getString(c.getColumnIndex(PlacesDBHelper.PHOTO_REFERENCE_COL)));
try {
URL url = new URL(urlString); // create new URL object from url string
new ImageDownloader(holder.icon, this).execute(url);
}catch (Exception e){
Log.e("Error in url", e.getMessage());
}
}
I am using chrisbanes pulltorefresh listview as well, im not sure if thats the casue for the problem
Bottom line question: how to make an item look steady and ready when he gets back into the screen and not beeign created again? ( cause even if i dont dowloand an image and only setting a bitmap, the action of setimagebitmap might be visible on a slow phone)
Thanks in advance!
The entire point of ListView is to recycle row views. They aren't destroyed, they are passed back to the adapter to be bound with new data for another row. It is expected that getView (or bindView in the case of cursor adapters) will be called again if you scroll an item off screen far enough and then scroll it back on screen.
What you need is an image caching layer between the download task and the adapter. When bindView happens, first check the cache to see if the image is there. If it is, use it. If it's not, download it and add it to the cache, then use it.
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.
So basically I have a function which is something like that :
public static Bitmap getBitmapFromURL(String src)
, which return decrypted Bitmap.I need to be able to use that Bitmap into a Lazy loading ListView.
Example :
So I have a ListView.I'm downloading encrypted images and use that getBitmapFromURL function to return them as Bitmap,and after that I want to be able to reload the ListView with the new images which getBitmapFromURL returns to me.I want to find a way to save them in cache so when there is like 50 loaded jpg's in ListView I want to be able to delete the first loaded and keep only these which are visible while scrolling the ListView.And do exactly the same thing when I have another 50 loaded images.Any Suggestions how I can do this?
This is the well-known lazyList :
http://open-pim.com/tmp/LazyList.zip
I have created the gridView with spinner I will upload it tomorrow and make it available (: Maybe create a tutorial ... I am off now I will keep you in mind