Skip to main content

Load Image from URL (Internet) in Android

https://www.viralandroid.com/2015/11/load-image-from-url-internet-in-android.html


In this tutorial, you will learn to load image from internet (url) in android imageview. Image won’t load just by adding URL. To load image from internet, you have to know little bit java code. Here is the easiest way to load image in android ImageView from URL.

Related:
How to Rotate an Image in Android
How to Make a Phone Call in Android
Android Material Design with Design Support Library

Android Example: How to Load Image from Internet (URL) in Android


This is the step by step guide to load image in android ImageView from URL/Internet.

Create a new Android Project

Application Name: Load Image From Internet
Company Domain: viralandroid.com
Package Name: com.viralandroid.loadimagefrominternet
Minimum SDK: Android 2.2 (API 8 Froyo)

XML Layout File
res/layout/image_from_url.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load Image From URL (Internet) in Android ImageView"
android:textSize="20sp" />
<ImageView
android:id="@+id/image_view"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_marginTop="16dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="ViralAndroid.com"
android:textSize="24sp"
android:gravity="center|bottom"
android:textStyle="bold" />
</LinearLayout>
view rawimage_from_url.xml hosted with ❤ by GitHub

Java Activity File
src/MainActivity.java
// How to Load Image From URL (Internet) in Android ImageView
package com.viralandroid.androidshorttutorial;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_from_url);
// Image link from internet
new DownloadImageFromInternet((ImageView) findViewById(R.id.image_view))
.execute("https://pbs.twimg.com/profile_images/630285593268752384/iD1MkFQ0.png");
}
private class DownloadImageFromInternet extends AsyncTask<String, Void, Bitmap> {
ImageView imageView;
public DownloadImageFromInternet(ImageView imageView) {
this.imageView = imageView;
Toast.makeText(getApplicationContext(), "Please wait, it may take a few minute...", Toast.LENGTH_SHORT).show();
}
protected Bitmap doInBackground(String... urls) {
String imageURL = urls[0];
Bitmap bimage = null;
try {
InputStream in = new java.net.URL(imageURL).openStream();
bimage = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error Message", e.getMessage());
e.printStackTrace();
}
return bimage;
}
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
}
}
view rawMainActivity.java hosted with ❤ by GitHub


You can add any image URL instead of above.

Add Internet Permission in AndroidManifest.xml File

To load image from internet/url you have to add internet permission in your AndroidManifest.xml file.
&lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt;
AndroidManifest.xml File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.viralandroid.androidshorttutorial">
<!--Internet permission to load image from url-->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
view rawAndroidManifest.xml hosted with ❤ by GitHub

Strings.xml File
Default content of strings.xml file.
res/values/strings.xml
<resources>
<string name="app_name">Load Image From Internet</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
view rawstrings.xml hosted with ❤ by GitHub

Now, run your application, your application looks like this.

Load Image from URL (Internet) in Android
Image From URL

Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap then to bytes. Also

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App §   2.4.7  Set the color §   2.4.8  Adding Animations o    2.5  To plot grouped bar chart §   2.5.1  Creating Dataset o    2.6  Get the best of Android plus exclusive deals and freebies in your inbox!