2010-01-20 65 views
3

谁能请我提供关于如何从一个网络服务器中保存图像,并将其设置为墙纸的一些想法/指导?我正在开发一个android应用程序,需要做到这一点,我是新的android。非常感谢。Android的 - 从Web服务器保存的图像,并将其设置为墙纸

我曾试图写我自己的代码,但我不能下载后发现我的图片,但墙纸已经改变下载的图片这是行不通的。这里是我现有的代码。

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

} 

回答

2

我曾试图写我自己的代码,但它作为 我无法下载后发现我的图片 不起作用。这里是我现有的 代码。

您的代码将图像保存在手机中的文件夹data/data/<your_app_package_name>。然后,您可以使用一个WallpaperManager instance或做context.setWallpaper(bitmap)(这是不建议使用)设置你的位图作为墙纸。

+0

我想将图像保存在SD卡中 – Lynnooi 2010-01-20 06:06:57

+0

好像你已经在你的代码中改变了几行:现在应该将图像保存在SD卡上。并且,将其设置为壁纸。 您是否收到任何错误? – Samuh 2010-01-20 06:22:26

+0

烨..我不得不做出一些改变我的代码。我没有收到任何错误,但我仍然无法在手机中找到图像。但是墙纸已经成功设置。 – Lynnooi 2010-01-20 06:24:13

相关问题