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"));
Related
I can get Facebook profile images using
http://graph.facebook.com/<facebookId>/picture?type=square
but it redirects to
https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/t1.0-1/p50x50/XXX.jpg
How can I set new image url using Loopj's Android Smart Image View?
You can the direct url first using the following code :
public String GetDirectURL(String url_send) {
URL url;
URL secondURL = null;
try {
url = new URL(url_send);
HttpURLConnection ucon = null;
try {
ucon = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ucon.setInstanceFollowRedirects(false);
secondURL = new URL(ucon.getHeaderField("Location"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return secondURL.toString();
}
and then assign the url returned to smart image view object ,but to not to be pixelated change type of image to large or xlarge
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.
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/
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;
}
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.