机器人 - 从Web服务器保存的图像,并将其设置为墙纸 [英] android - save image from web server and set it as wallpaper
问题描述
任何人都可以请提供我如何保存图像从一个网络服务器,并将其设置为墙纸的一些想法/指导?我在开发的需要做了一个Android应用程序,我新的机器人。非常感谢。
我曾试图写我自己的code,但它不工作,我不能下载后发现我的图片,但墙纸已经改变下载的图片。这里是我现有的code。
位图bmImg;
无效downloadFile(字符串fileUrl){
URL myFileUrl = NULL;
尝试 {
myFileUrl =新的URL(fileUrl);
}赶上(MalformedURLException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
尝试 {
HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl
.openConnection();
conn.setDoInput(真正的);
conn.connect();
INT长度= conn.getContentLength();
InputStream的是= conn.getInputStream();
bmImg = BitmapFactory.de codeStream(是);
// this.imView.setImageBitmap(bmImg);
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
尝试 {
字符串的文件路径= Environment.getExternalStorageDirectory()getAbsolutePath()。
的FileOutputStream fos基因=新的FileOutputStream(文件路径+/+output.jpg);
bmImg.com preSS(比较pressFormat.JPEG,75,FOS);
fos.flush();
fos.close();
上下文的背景下= this.getBaseContext();
context.setWallpaper(bmImg);
}赶上(例外五){
//Log.e("MyLog,e.toString());
TextView的电视=(TextView中)findViewById(R.id.txt_name);
tv.setText(e.toString());
}
}
我曾试图写我自己的code,但它 不工作,因为我无法找到我的图片 下载后。这里是我现有的 code。
您code将保存在数据/数据/&LT形象; your_app_package_name>在手机
文件夹。然后,您可以使用一个 WallpaperManager实例
或做 context.setWallpaper(位图)
(这是德precated)设置你的位图作为壁纸。
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.
这篇关于机器人 - 从Web服务器保存的图像,并将其设置为墙纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!