Images getImageUrl()

Asked

Viewed 931 times

1

I’m uploading images like this:

final ImageView img = (ImageView) firstElementView.findViewById(R.id.grid_image);
String src = item.getImageUrl(); 
img.setTag(src);
imageLoader.displayImage(src, img, Utils.getImageLoaderOptions());

But now I need to search images by url through an API, that is, the url is not available directly, you need to make a call first by httpurlconnect().

Solutions? Do download of the via image httpurlconnect and then show the image? How to do it this way? What other solutions are there?

3 answers

1

Resolved with:

I used:

Bitmap bitmap = BitmapFactory.decodeStream(conn.getInputStream());

to get the image in Bitmap and then used:

imageView.setImageBitmap(bitmap);

0

  • Thanks for the answer, it was easier than it looked. I used: Bitmap bitmap = Bitmapfactory.decodeStream(Conn.getInputStream(); to get to Bitmap and then used: imageView.setImageBitmap(bitmap);

0

You can use the Picasso.

Picasso is a very popular and used library in Android development that solves all the problem of loading and processing images for you and also simplifies the display of third party images (such as URL’s). The Picasso makes from the requisition (HTTP) up to the cache of this image for future use.

1) Add the Picasso in his build.gradle and Sincronize your project:

compile 'com.squareup.picasso:picasso:2.5.2'

2) If not, add the internet permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>

3) Create a ImageView any to display your image:

<ImageView
    android:id="@+id/suaImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

4) Now, on your Activity (or Fragment or any other place that wants to display an image) just reference your ImageView and tell the Picasso upload your URL:

ImageView suaImageView = (ImageView) findViewById(R.id.suaImageView);

Picasso.with(this)
        .load("http://www.dominio.com.br/imagem.jpg")
        .into(suaImageView);

You also have the possibility to manipulate your image, for example:

Picasso.with(context)
  .load(url)
  //Definindo uma resolução para sua imagem
  .resize(50, 50)
  //Definindo um Scale Type para a sua imagem
  .centerCrop()
  .into(imageView)

Links:

Official page of Picasso: http://square.github.io/picasso/

Page of GitHub of Picasso: https://github.com/square/picasso

  • Thanks for the answer, it was easier than it looked. I used: Bitmap bitmap = Bitmapfactory.decodeStream(Conn.getInputStream(); to get to Bitmap and then used: imageView.setImageBitmap(bitmap);

  • @Micaelcosta be careful with that approach! This can "freeze" the user screen, since, loading images, is considered a heavy task to run on the main thread (UI thread). If you are really going to implement it this way, I recommend reading this article http://developer.android.com/training/displaying-bitmaps/index.html (Remembering that Picasso already solves this whole problem for you)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.