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

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.

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"));

Dynamically populate Android ImageView with outside resources

How can I turn the static image
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.67"
android:src="#drawable/static_image" />
into an ImageView whose source can be dynamically set to data that is not already in the res folder?
That is, my application has an icon on the screen but the actual image for the icon is downloaded from an outside server and can change dynamically. How do I update the ImageView with the desired image upon download? I want something functionally like:
Image selectedImage = //get from server
myImageView.setImage(selectedImage);
Ur question is not clear. If u just wanna have an image(that is in some url) set to an image view,
Bitmap bmp=getBitmapFromURL(ur url here);
imgview.setImageBitmap(bmp);
and write this function:
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 (Exception ex) {
return null;
}
yourImageView.setImageBitmap(bitmap);
to get Bitmap from server:
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
As per I understand your question something like,
ImageView selectedImage;
selectedImage = (ImageView)findViewById(R.id.imageView1);
Bitmap bmImg;
downloadFile(imageUrl);
And this is downloadFile() Method...
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);
selectedImage.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You need to create a custom view and over-ride onDraw(). From there you are provided the drawing Canvas and you can do whatever you like. So assuming your dynamic image can be converted to a bitmap, there are numerous drawBitmap() methods you can use from Canvas.
Note that you must also override onMeasure() which allows your view to tell the layout manager how much space you need.
There's a lot of good info here:
http://developer.android.com/guide/topics/ui/custom-components.html

Android :loading remote images in application

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/

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;
}

Categories

Resources