How to Display Image from Url on Android

How to display image from URL on Android

You can directly show image from web without downloading it. Please check the below function . It will show the images from the web into your image view.

public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}

then set image to imageview using code in your activity.

Load image from url

URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);

Display image from URL into ImageView?

Ok I added it and included the .jar but it doesnt work, the app just crashes, I think this is the relevant part to solve this issue:

try {


mComments = json.getJSONArray(TAG_POSTS);

// looping through all posts according to the json object returned
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);

//gets the content of each tag
String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_MESSAGE);
String username = c.getString(TAG_USERNAME);
String url = c.getString(TAG_URL);


// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

map.put(TAG_TITLE, title);
map.put(TAG_MESSAGE, content);
map.put(TAG_USERNAME, username);
map.put(TAG_URL, url);


// PART TO GET IMAGES - "Can not resolve context"

ImageView imageView = (ImageView) findViewById(R.id.url);
Picasso.with(context).load(url).into(imageView);



// adding HashList to ArrayList
mCommentList.add(map);

//annndddd, our JSON data is up to date same with our array list
}

EDIT

When I started to build the app, I created 2 posts with direct file paths of the images, where they are stored at the device, they are displayed correct:
Screenshot

The uploaded images are not displayed, they have a url like this:

"http://www.eywow.com/webservice/uploads/name.jpg"

Android display images from url in listview

Look at this article http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/

The is an example of fetching and displaying of images using Volley library.

Well the problem is in your ListAdapter. SimpleAdapter just show fields as strings.

Implement your own Adapter with custom view.

How to display images from URL efficently

Since the images are stored on a remote server there is no way to bypass the downloading process, however Glide makes sure to only download the image from remote server when necessary, as the docs state

By default, Glide checks multiple layers of caches before starting a
new request for an image:

Active resources - Is this image displayed in another View right now?
Memory cache - Was this image recently loaded and still in memory?
Resource - Has this image been decoded, transformed, and written to
the disk cache before? Data - Was the data this image was obtained
from written to the disk cache before? The first two steps check to
see if the resource is in memory and if so, return the image
immediately. The second two steps check to see if the image is on disk
and return quickly, but asynchronously.

If all four steps fail to find the image, then Glide will go back to
the original source to retrieve the data (the original File, Uri, Url
etc).

To resolve this,

but i don't like the loading effect it makes (blank space while
loading, and the the image)

You can instead add a default placeholder on the ImageView until the real image is downloaded. This will display a default image while your actual image is downloading and then after download completion will replace it with the actual one.

Glide
.with(context)
.load(url)
.placeholder(R.drawable.ic_placeholder) //your default placeholder resource
.into(imageView)

Android App Display Image From URL

Here, this is how I display image from url in the image view

you have to call this code from thread other than main thread

ImageView img = (ImageView) findViewById(R.id.imageView1);
try {
URL url = new URL("Your URL");
//try this url = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"
HttpGet httpRequest = null;

httpRequest = new HttpGet(url.toURI());

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);

HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();

Bitmap bitmap = BitmapFactory.decodeStream(input);

img.setImageBitmap(bitmap);

} catch (Exception ex) {

}

Be careful don't forget to surround the code with try catch(I have already done that in this code)

or you can use webview to load image from url

WebView web = (WebView) findViewById(R.id.webView1);
web.loadUrl("Your Url");

if you are trying to load image from the assets folder url will start like this
"file:///android_asset/yourimage.jpg"

else normal internet url like this
"http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"

hope this works for you
Good Luck

I want to show a picture from URL in my recyclerview

You can use Picasso library for the same.

Permission in android manifest

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

Add Picasso in app level build.gradle

implementation 'com.squareup.picasso:picasso:2.5.2'

Loading image

Picasso.get().load(model.getTrack().getAlbum().getImages().get(position).getUrl()).into(holder.songCover);

Why Can't I Show an Image From Url in Android Studio?

As @Ashis said, my problem was solved when I added the following line in the manifest.

android:usesCleartextTraffic="true"


Related Topics



Leave a reply



Submit