Android :loading remote images in application - android

I needed to load remote images to my application and bind this image to an QuickcontactBadge object. HttpURLConnection is used to download the image data and BitmapFactory is used to produce the bitmap which will be used as imageview resources.
But I dont know exactly how yo do this?
Can anyone help me over this?
Thanks

This could help you.
Bitmap bmImg;
void downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Source : http://en.androidwiki.com/wiki/Loading_images_from_a_remote_server
See this too
http://ballardhack.wordpress.com/2010/04/05/loading-remote-images-in-a-listview-on-android/

Related

Android - Can't load image from http

I need to load an image from http, and I'm using this code:
Bitmap bitmap;
InputStream is = null;
try {
is = (InputStream) new URL("www.TESTWEBSITE.com/TEST.JPG").getContent();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap = BitmapFactory.decodeStream(is);
But bitmap is still null.. Any help please ?
Update : this is full snapshot performing what you want :
Bitmap bitmap = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL("www.TESTWEBSITE.com/TEST.JPG");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
Please, make sure to run it off UI thread, such as in AsyncTask as others have commented. You can try it in main thread for experimental purposes but be prepared for ANR.

Download Image from web

I have images on server named by email so when i try to download them on my application the # symbol not shown on the path, is there anyway to stop escape this symbol ?
Example :
Correct Path
Http://www.xxxxx.com/a#a.com.jpg
Wrong Path
Http://www.xxxxx.com/aa.com.jpg
I tried URL encode but its not useful in my case
Bitmap downloadFile(String fileUrl) {
URL myFileUrl = null;
Bitmap bmImg = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
// imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmImg;
}
Try using the URLEncoder from Android SDK.
try {
myFileUrl = new URL(java.net.URLEncoder.encode(fileUrl));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EDIT:
Just saw my mistake. That won't work because it's encoding the entire URL.
You have to separate the email address from the rest of the url like this.
myFileUrl = new URL("http://www.xxxx.com/"+java.net.URLEncoder.encode(email));
Or alternatively, just replace the problem character.
myFileUrl = new URL(fileUrl.replace("#","%40"));

Show Avatar Facebook to ImageView

I used the code as follows to show up facebook avatar to ImageView
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.imgAvatar);
img.setImageBitmap(getBitmapFromURL("http://graph.facebook.com/"+"100002394015528"+"/picture"));
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}`
But does it not work. Please help me.
http://graph.facebook.com/id/picture doesn't return an image. It returns some response headers including a 302 redirect, and a location header.
Your example for instance redirects to: http://profile.ak.fbcdn.net/hprofile-ak-snc4/211619_100002394015528_568817_q.jpg
So instead of
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
You need to get the headers from the request, follow the location and then do what you were doing before. I don't know Android, or what language that is. (Java?) So I can't help with that, but I think this might be enough information to get you headed in the right direction.
Use this function for get real URL to avatar:
public static String getUrlFacebookUserAvatar(String name_or_idUser )
{
String address = "http://graph.facebook.com/"+name_or_idUser+"/picture";
URL url;
String newLocation = null;
try {
url = new URL(address);
HttpURLConnection.setFollowRedirects(false); //Do _not_ follow redirects!
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
newLocation = connection.getHeaderField("Location");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newLocation;
}

streaming image from url, android

I need to load, and update image from URL.
Using AsyncTask, Iam able to load image from URL bt i need to reload image from url for every 10 secs.
Please help me how i can solve this issue.
Thanks in advance
#Praveenb
try following ,
Bitmap bmImg;
void downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
// it will decode the input stream and will load the bitmat in bmImg variable
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
following code works fine for me,
class DownloadImage extends AsyncTask<Void, Void, Drawable>{
#Override
protected Drawable doInBackground(Void... params) {
return Util.getImageFromURL(imageURL);
}
#Override
protected void onPostExecute( Drawable d ) {
getImageIcon().setImageDrawable(d);
}
}
new DownloadImage().execute();
and if you are showing image in list view you should follow this
http://github.com/commonsguy/cwac-thumbnail

android - save image from web server and set it as wallpaper

Can anyone please provide me some idea/guidance on how to save an image from a webserver and set it as wallpaper? i am developing an android application which needs to do that and i am new in android. Thanks a lot.
I had tried writing my own code but it doesn't work as i can't find my images after download but the wallpaper has change to the downloaded picture. here is my existing code.
Bitmap bmImg;
void downloadFile(String fileUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
// this.imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String filepath=Environment.getExternalStorageDirectory().getAbsolutePath();
FileOutputStream fos = new FileOutputStream(filepath + "/" + "output.jpg");
bmImg.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
Context context = this.getBaseContext();
context.setWallpaper(bmImg);
} catch (Exception e) {
//Log.e("MyLog", e.toString());
TextView tv = (TextView) findViewById(R.id.txt_name);
tv.setText(e.toString());
}
}
I had tried writing my own code but it
doesn't work as i can't find my images
after download. here is my existing
code.
Your code would save the image in the data/data/<your_app_package_name> folder of the phone. You can then use either a WallpaperManager instance or do a context.setWallpaper(bitmap)(this is deprecated) to set your bitmap as the wallpaper.

Categories

Resources