Today I will show you how to Show Images Remotely (Hosted on a web server or Website) Or from URL in android.
It is very Difficult to Load Images From Remote Server Because there is No Such built-in support in IMAGE VIEW Control of android .
But now you Can Easily Access Images Remortely in android .
I researched A lot and Finally got a Suitable Coding technique for Accessing Images from Website(Remotely) in Image View android.
One Major Advantage with my code is that allows you to access any image format e.g .png , .jpg , .bmp or .gif.
One Important thing to Understand Firstly is that there is a method in ImageView Class ImageView.setImageURI(URI uri) method , We cannot use it For Showing Remote Images. It is only used for instance if the uri contains a reference to a local file.
Eg: file:///sdcard/images/thumb.png. In Sample Code .In sample code "SHOW2" button Utilize setImageURI() method.
So Lets Start,
Follow these Step,
First Create A new Project in android and in Activity Class And Write Or Copy Following Code.
and Code in Main.xml File is Below
.GIF IMAGE OUT PUT.
DOWNLOAD SOURCE CODE (GOOGLE)
OR
DOWNLOAD SOURCE CODE (4Share)
It is very Difficult to Load Images From Remote Server Because there is No Such built-in support in IMAGE VIEW Control of android .
But now you Can Easily Access Images Remortely in android .
I researched A lot and Finally got a Suitable Coding technique for Accessing Images from Website(Remotely) in Image View android.
One Major Advantage with my code is that allows you to access any image format e.g .png , .jpg , .bmp or .gif.
One Important thing to Understand Firstly is that there is a method in ImageView Class ImageView.setImageURI(URI uri) method , We cannot use it For Showing Remote Images. It is only used for instance if the uri contains a reference to a local file.
Eg: file:///sdcard/images/thumb.png. In Sample Code .In sample code "SHOW2" button Utilize setImageURI() method.
So Lets Start,
Follow these Step,
First Create A new Project in android and in Activity Class And Write Or Copy Following Code.
package imagesFrmURLSAJJADASHRAFpackage.namespace;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
public class ImagesFrmURLSAJJADASHRAFActivity extends Activity {
/** Called when the activity is first created. */
//controls
EditText inputUrl;
ImageView imgView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Get control view
imgView = (ImageView)findViewById(R.id.imageView1);
inputUrl = ((EditText)findViewById(R.id.editText1));
inputUrl.setSingleLine();
inputUrl.setTextSize(11);
}
public void SHOW_Click2(View view) throws IOException
{
//This Block of Only Use When You Refrencing Local File not for Remorte
Uri imgUri=Uri.parse(inputUrl.toString());
imgView.setImageDrawable(null);
imgView.setImageURI(null);
imgView.setImageURI(imgUri);
/*//Work for short Images
URL thumb_u = new URL(inputUrl.toString());
Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
imgView.setImageDrawable(thumb_d);*/
//imgView.setImageDrawable(Drawable.createFromStream(getContentResolver().openInputStream(imgUri),null));
}
public void SHOW_Click(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.imageView1);
imgView.setImageDrawable(image);
}
//Below Method Normally Return drwaable Images form URL or Web server
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;
}
}
//This Method Return Content Prent on URL No metter Weather it is a GIF, .JPG,.png
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
}
and Code in Main.xml File is Below
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter URL of Image"
android:text="http://moiz7860.tripod.com/sitebuildercontent/sitebuilderpictures/asmaaulhusna.jpg" >
<requestfocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW 1"
android:onClick="SHOW_Click"
android:paddingLeft="5dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW 2"
android:paddingLeft="5dp"
android:onClick="SHOW_Click2" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
One Important Reminder : - Please Don't forget add Permission for Using Internet in android in AndroidMainFest.XML file. Code is Below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sajjadAshraFpackage.namespace"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ImageGridViewVisualStudioLearnActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- FullImageActivity -->
<activity android:name=".FullImageActivity"></activity>
</application>
</manifest>
SAMPLE OUT PUT:.GIF IMAGE OUT PUT.
DOWNLOAD SOURCE CODE (GOOGLE)
OR
DOWNLOAD SOURCE CODE (4Share)











0 comments:
Post a Comment