我有一個圖像按鈕,並希望從互聯網URL設置背景圖像。我不想將背景圖片保存到SD卡,而是我的圖像按鈕的圖像需要URL。我怎樣才能做到這在Android中如何在Android中設置網頁圖片按鈕資源?
0
A
回答
0
從未嘗試過這一點,但完全希望這將與您
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, saveFilename);
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
和圖像視圖
ImageView IV= (ImageView)findViewById(R.id.imageId);
Drawable drw = ImageOperations(this,url,filename)
IV.setBackgroundDrawable(drw)
獲取URL
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
0
嘗試工作這
Bitmap bitmap;
class loadImage extends AsyncTask<Void , Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
URL url = new URL(stringURL);
URI uri = new URI(url.getProtocol(), url.getHost(),
url.getPath(), url.getQuery(), null);
HttpURLConnection connection = (HttpURLConnection) uri
.toURL().openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = input.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
byte[] img = byteBuffer.toByteArray();
byteBuffer.flush();
byteBuffer.close();
input.close();
bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ImageButton image_btn = (ImageButton)findViewById(R.id.your_image_button_id);
image_btn.setImageBitmap(bitmap);
}
}
0
首先,你必須下載映像,作爲繪製對象: Android Drawable Images from URL
,之後將其設置爲背景繪製
button.setBackgroundDrawable(drawable)
相關問題
- 1. 更改按鈕上的圖像資源設置點擊片段
- 2. 如何在其他課程資源中設置圖片視圖?
- 3. 如何設置圖片上傳按鈕?
- 4. 在UserControl按鈕中設置圖像源
- 5. 如何在android中設置資源文件夾的背景圖片?
- 6. 如何在Android中設置按鈕?
- 7. 如何設置圖片按鈕(Android)的文字?
- 8. 如何在片段中設置按鈕偵聽器按鈕
- 9. 如何在ListView Android中設置圖像資源?
- 10. 設置圖片來自資源文件
- 11. 如何設置背景圖片的ClientBundle資源在UiBinder的(GWT)
- 12. 如何爲Android按鈕設置背景圖片,但保持灰色按鈕?
- 13. 爲我在XML(Android)中製作的按鈕設置圖片?
- 14. 使用android時設置圖片的一面:drawable ____在按鈕中
- 15. 如何在按鈕的中心設置圖片和文字
- 16. 圖片資源在Android中給出0
- 17. Android圖片資源縮小
- 18. 如何在圖像按鈕上設置datapicker單擊android中的片段?
- 19. 如何使用資源文件設置按鈕文本
- 20. 網頁按鈕點擊圖片更新
- 21. 如何在按鈕中設置文本中的圖像 - android?
- 22. 的Android的Java設置圖像資源
- 23. 如何將位圖圖像設置爲按鈕背景圖片
- 24. 如何將圖像源設置爲「圖片」庫中的圖片?
- 25. Android圖庫視圖 - 如何設置資源?
- 26. 如何設置數值並按下網頁上的按鈕?
- 27. 如何在android中設置UIControlStateSelected的按鈕圖像?
- 28. android-如何在列表視圖中設置按鈕參數
- 29. 如何設置圖像資源?
- 30. Android:如何在應用程序中設置按鈕的按鈕圖像
你需要將它下載到一個位圖變量,然後將其設置爲背景圖片。 Android不是您想要實現的功能的瀏覽器。 – abbath 2013-03-04 11:15:52
我認爲你的答案就在這裏 http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – 2013-03-04 11:24:19
@MudassarShaheen我覺得一個評論是不夠的同樣的鏈接:) – 2013-03-04 12:22:45