Set a Parsed (JSON) image into background of a layout? [duplicate] - android

This question already has answers here:
Android - How to download an image and use it as new resource?
(4 answers)
Closed 9 years ago.
I have got a demo image link :
http://madhabpurps.org/wp-content/uploads/2013/04/28-239x300.jpg
I want to set the image in the background of a layout inside the view holder class:
static class ViewHolder {
TextView txtName;
TextView txtCityState;
RelativeLayout rl;
}
holder.txtName.setText(searchArrayList.get(position).getTitle());
holder.txtCityState.setText(searchArrayList.get(position).getDescription());
I have to set the image from the link here, I have tried this line of code but it's showing error.
holder.rl.setBackgroundResource(searchArrayList.get(position).getImage());

I get answer from here change background image of Framelayout via URL
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, saveFilename);
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
and then you can use it like this
Drawable drw = ImageOperations(this,url,filename)
rl.setBackgroundDrawable(drw);
This should fix. But for general case I recommend another way to solve these problems.
I recommend using a well documented image downloading and caching library. I am using picasso http://square.github.io/picasso/ . Setup it, using library is easy.
Then you can fill an imageview by just writing
Picasso.with(activity)
.load(url)
.fit()
.into(imageView);

use the below code
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
Drawable d = new BitmapDrawable(getResources(),bitmap);
rl.setBackgroundDrawable(dr);

Related

Android imageview flickering

In my app I have list view and each item in the list contains an Image view. The image is loaded from bitmap using
imageView.setImageBitmap(bitmap);
For handling concurrency i am using the method described in the following official documentation.
Processing Bitmaps Off the UI Thread
On high density devices the image view flickers. Any possible solution for this problem?
when i use handle concurrency then only the flickering happens and when i am using asynctask only then there is no flickering
Use Picasso , Picasso saves you all the problems with downloading, setting and caching images. The whole code needed for a simple example is:
Just use like this (from UI Thread) :
In listview , use this in getView() , it handles caching and else..
Picasso.with(context)
.load(url)
.into(imageView);
http://square.github.io/picasso/
or
ImageView tre = (ImageView) findViewById(R.id.imageview);
String URL = "http://www...sdsdsd ...";
mChart.setTag(URL);
new DownloadImage.execute(tre);
public class DownloadImage extends AsyncTask<ImageView, Void,Bitmap> {
ImageView imageView = null;
#Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
#Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
Bitmap bmp =null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
}catch(Exception e){}
return bmp;
}

Android: Load image on launch from URL

I am a learner and have been working on a home visitor app to get image from a URL of the visitor. Using the following code, which I found online, I was able to load image by adding an intent before the screen and adding a button on the screen saying "See Visitor Image" but now I want that my image should load as soon as the app launches. What changes could I make to do that? Thanks for your help.
OnClickListener getImageBtnOnClick = new OnClickListener() {
public void onClick(View view) {
Context context = view.getContext();
Editable ed = inputUrl.getText();
Drawable image = ImageOperations(context,ed.toString(),"image.jpg");
ImageView imgView = new ImageView(context);
imgView = (ImageView)findViewById(R.id.image1);
imgView.setImageDrawable(image);
}
};
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
inputUrl = ((EditText)findViewById(R.id.imageUrl));
inputUrl.setSingleLine();
inputUrl.setTextSize(11);
Button getImageButton = (Button)findViewById(R.id.getImageButton);
getImageButton.setOnClickListener(getImageBtnOnClick);
}
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
You can make use of SmartImageView, it is a drop-in replacement for Android’s standard ImageView which additionally allows images to be loaded from URLs or the user’s contact address book. Images are cached to memory and to disk for super fast loading.
https://github.com/loopj/android-smart-image-view
Or on OnResume of ur activity start downloading the image form the url as u r doing now in the click listener of button in a separate thread to avoid blocking main UI thread. Once you completed the download you can update the image view using handler from the worker thread.
You can make use of async task also instead of creating your own thread and handler to update UI. for more infor about async task refer following link http://www.vogella.com/articles/AndroidPerformance/article.html

ImageView not updating with bitmap?

why my imageview is not updating in the for loop,only the image from last url is showing in the imageview not all the images,what i am doing wrong?
public class LoadImageActivity extends Activity {
ImageView image_view;
Bitmap bitmap;
String[] imageLocation={
"http://wallbase1.org/thumbs/rozne/thumb-499842.jpg",
"http://ns3002439.ovh.net/thumbs/rozne/thumb-2493796.jpg",
"http://ns3002439.ovh.net/thumbs/rozne/thumb-2486664.jpg" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image_view = (ImageView)findViewById(R.id.imageview);
for (int i = 0; i < 2; i++) {
bitmap = loadImage(imageLocation[i]);
image_view.setImageBitmap(bitmap);
Animation rotate = AnimationUtils.loadAnimation(LoadImageActivity.this, R.anim.rotate);
findViewById(R.id.imageview).startAnimation(rotate);
}
}
public Bitmap loadImage(String image_location){
URL imageURL = null;
try {
imageURL = new URL(image_location);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection = HttpURLConnection)imageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
Well, at first, how slow is you device that you would switch between images in two successing lines of code (effectively)?
Secondly, the system has to be layouted first which happens after onCreate() returns. What you did is:
-set reference to bitmap that will be shown when layouted
-changed that reference
-let the system do the layout (Question: Which reference will be read?)
It looks like you want to set the first bitmap, rotate it and then set the other bitmap.
What you need to do is:
-set bitmap
-start animation with an Animation.AnimationListener, which sets the second bitmap in onAnimationEnd().
#Thanks to Adam!
You can put an Animation.AnimationListener on your first animation, and you can load the next image and start the new transition in the onAnimationEnd(...) method of the listener.
Try setting the underlying object (Bitmap) and then updating the ImageView.
The method loadImage() is not in UI main Thread, use a Handler to update the ImageView with your new image.

Internet url as Android ImageSpan source

is it possible to specify internet url for ImageSpan and get it shown with TextView? I've tried quite a few versions of
String mockContent = "<img src=\"http://www.google.com/intl/en_com/images/srpr/logo3w.png\">";
myTextView.setText(Html.fromHtml(mockContent), BufferType.SPANNABLE);
but that results in the generic not found image (blueish black bordered square).
Do I have to download the image first, then replace all the urls and so on?
Knowing the new C# is capable of handling such resources, I hoped Android might meet my hopes here.
Using API version 10: 2.3.3.
If you have the image source locally then it should work or else use,
Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
Returns displayable styled text from the provided HTML string.
Nice example I found on web,
String s = Html.fromHtml(htmlContent, new ImageGetter() {
#Override
public Drawable getDrawable(String source) {
// Code for getting image either by download or using static iamge
Drawable d = null;
try {
InputStream src = imageFetch(source);
d = Drawable.createFromStream(src, "src");
if(d != null){
d.setBounds(0,0,(d.getIntrinsicWidth(),
d.getIntrinsicHeight());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return d;
}
},null);
For more info Html.fromHtml with ImageGetter.

ListView scrolling lag in android

I am having a customized list view in my application, which is showing an image and text.
The Image I am getting from URL, using the code below:
private static Drawable ImageOperations(Context ctx, String url,
String saveFilename) {
try {
InputStream is = (InputStream) fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
all is working perfect, except the list view scrolling, its very slow. If I disable the images, the scroll speed smooth-ens , but with the image enabled, it lags alot.
Is there any possible way I can reduce or remove this lagging?
Thanks
You need to do your fetching in the background.
One of the examples you can use :
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
use this library for downloading images in background and caching..
it won't hurt the UI
https://github.com/koush/UrlImageViewHelper
Are you lazy loading your images? See this question for details.

Categories

Resources