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());
}
}
我想將圖像保存在SD卡中 – Lynnooi 2010-01-20 06:06:57
好像你已經在你的代碼中改變了幾行:現在應該將圖像保存在SD卡上。並且,將其設置爲壁紙。 您是否收到任何錯誤? – Samuh 2010-01-20 06:22:26
燁..我不得不做出一些改變我的代碼。我沒有收到任何錯誤,但我仍然無法在手機中找到圖像。但是牆紙已經成功設置。 – Lynnooi 2010-01-20 06:24:13