Displaying an image from the internet with android - java

I am writing on an android app, and my current task is to load an image from a URL and display it in an ImageView.
What i did so far: I managed to download the image and save it as a bitmap. Now when i want to add it to an intent via
intentRecord.putExtra("Data",(DownloadDataImage) result);
where result is my bitmap image, i get an error because a bitmap is not serializable, and .putExtra() requires a serializable input.
Do you guys know any workaround for me? I don't know how to circumvent my problem in an appropriate way.
Is there a way of adding an image to an imageView without using a Bitmap, and instead using another serializable type? Or is there a smooth way of making my bitmap serializable? I know this is possible, but i am not quite sure how to do this.
Thx a lot for your help!

I suggest you to try picasso to do the hard work of handling/caching images.
Once you have the jar in your workspace, you just need one line of code
Picasso.with(context).load(image_url).into(imageView);

Because there is an upper bound to the size of what can be put in Intent extras, you should look into another way to get the image into your ImageView.
At a very high level the steps of this could be:
IntentService downloads image and stores it somewhere that's globally accessible (in-memory cache or on disk).
After download has completed and service has saved the image somewhere, it sends a broadcast to let BroadcastReceivers in your app know that the image is now available.
A BroadcastReceiver in the activity or fragment that is hosting your ImageView would then retrieve the image from the stored location and place it inside the ImageView (if from disk then remember to do this retrieval off the main thread).
As other answerers have mentioned, there are frameworks that do a lot of this boilerplate work. Picasso, as suggested by droidx, is a great solution that's easy to implement and would probably be your best bet.

you can use external library like Universal Image Loader and you won't have to worry about cache management and other things...

Related

How to add a 360 degree panorama view to an android app?

Well, I have been searching a way to add a 360 degree panoramic image for now, but for some reason, I was not able to find something useful from the internet. The only thing I found was this tutorial was this Tutorialhttps://youtu.be/gnDa-fvEyUk, which, unfortunately, is in Kotlin. So hope you guys would help me sort this.
Well, by the way I found this documentaion; however, I was not able to comprehend much from this, especially the java part. It seems the layout part is easy(all about adding VRPanorama viewGoogle VR documentation
Thanks in advance.
PS: I am gonna do it in a fragment, so if possible, please show me the way of fragments.
EDIT: In general, what I want to add is 360-degree to one of my fragments that is similar to the video I shared. I have already added the dependencies and wrote the code for the layout file(the same as the tutorial), and also, I am more interested in URL way. After some searching, again I found something which might be helpful(Still Kotlin though). Tutorial
To retrieve image from the URL, all you should do is add a invisible i.e gone imageview in the xml and using glide load the image in that view and from the view retrieve that image as drawable and convert it to Bitmap and then load that view in the VR's SDK method and load it.
That's it, and for the fragment part all you have to do is define the VR and Image views in the fragment and then write the required code in the fragments class.

How to get image from PhotoEditor SDK

I am trying to integrate my project with PESDK. I need to get image that is currently in the editor. I found that PESDK has R.id.editorImageView. This is object of EditorPreview class which isn't an extension of ImageView. So I didn't understand how to extract the image. Because of word Preview I suppose that PESDK do not apply immediately all changes to the image and in the end after pressing default AcceptButton of PESDK it runs final calculations and saves this image.
Is there any way that I can emulate this action? I just need to get Bitmap in the memory without saving it.
If there is no such way then I think the one possible solution is to call onClick of default AcceptButton and notify my code that it was pressed myButton, not AcceptButton. Then read saved image, process it and then delete file. I'm quite new in Android and if you know how to do this notification (that it was myButton pressed) then please share your knowledge.
Thank you for any help or advice!

When I use Picasso to load image into ImageView, then setImageBitmap becomes has no effect

I have used Picasso lib to load images from server and it works fine for me but when I want to select another image from Gallery to load it into the same image view nothing happens.
Here is my code
Picasso.with(EditProfileActivity.this)
.load(User.getInstance().getProfilePicPath())
.placeholder(R.drawable.defaultpp)
.error(R.drawable.defaultpp)
.into(imageView);
By the way when I comment the above line, I can select an image and show it into the image view, so can anyone give the reason of what happened ?
If you want to load images from gallery, you can pass file URI into Picasso
Picasso.with(getActivity()).load(fileUri).into(imageView);
You also have to make sure that you have enough permissions to fetch the given image.
This is an issue with the picasso lib, and has been answered by the creator itself, though solved in the latest version (but is still in alpha), a work around for picasso with Uri is this:
Picasso.with(this).load("file://" + User.getInstance().getProfilePicPath()).placeholder(R.drawable.placeholder)
.config(Bitmap.Config.RGB_565).into(imageview);
Make sure you dont forget the file:// prefix
Ref: the issue tracker for Picasso here
Because of when you select image your activity is sended back and imageviews route is lost. So yo have to define it again when it returns to your activity. add onResume or add onRestoreInstanceState then define imageview again ex: imageview findview... etc. It is the problem.

How to set an array list to image view present inside a listview

I have a listview which contains ImageView i. Now I have an array list which contains URLS inside it like www.abc.jpg, www.def.jpg and so on. Now I want to insert these URLs as the source for the image view. Is it possible.?
ImageView doesn't have any API that allows setting of urls that will be downloaded later on. You need to separately from the ImageView setup an asynchronous task that will download this image from the url, convert it to a Bitmap and then set it on ImageView with setImageBitmap(Bitmap).
As noted, you're doing network I/O in the main thread. The stack trace identifies it as happening in onPostExecute. In there, I see this code:
BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()
That certainly looks like network I/O to me.
What I'm wondering is, you've got this lovely worker thread already, but you're doing a lot of work in onPostExecute() (main thread). To prevent janky UI, I suggest you move as much of that computation into doInBackgroud() as possible.

When to recycle bitmap in android project?

I have successfully implemented lazy loading of list images and list items in Android listview. I am using Android 4.0+ and Java 7.
The algorithm i followed is:
List data(including image URL) is downloaded from internet as and when user scrolls the list.
When scroll state is idle, list images are loaded.
In the background thread, images are first checked for in the cache. If not present in cache, they are downloaded and stored into the cache.
Lastly image is set to imageview in the listview and adapter is notified.
The only problem is I am not clear about when to recycle bitmaps. I tried using bitmap.recyle() at many places but I got the following error:
java.lang.IllegalArgumentException: Cannot draw recycled bitmap
It is not possible to add that vast code over here. Also there are some privacy issues. Can someone please help me about this?
EDIT
My application size increases from 727 KB (at the time of installation) to 14 MB.
After I recycle my bitmaps, in getView() of adapter I get "cannot generate texture from bitmap ".
Can anyone suggest how to get rid of it?
Recycling a bitmap renders it unusable. Only recycle when you're completely done with it. In your case, that means after it's been evicted from the cache. You'll also want to make sure that none of your existing views reference it.
As of ICS the need to recycle isn't necessary. There are a few instance where you would want to but considering most listview implementations it probably won't be necessary.
You can check out this video by Chet Hasse for more info on reusing bitmaps which would be better if they are the same size. DevBytes: Bitmap Allocation
Bitmap recycling should be performed differently in different versions of Android. It is best to implement in a way that covers the majority of versions.
As others here said, recycle() renders your bitmap unusable, recycle() is meant to be used once you are finished with the bitmap and would like to induce a garbage collection. I think you should use it on your activity onPause()/onStop().
See here for more information:
Managing Bitmap Memory

Categories

Resources