我有一個圖像按鈕,並希望從互聯網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. 如何在android中設置資源文件夾的背景圖片?
- 5. 如何在Android中設置按鈕?
- 6. 如何設置圖片按鈕(Android)的文字?
- 7. 如何在片段中設置按鈕偵聽器按鈕
- 8. 設置圖片來自資源文件
- 9. 如何爲Android按鈕設置背景圖片,但保持灰色按鈕?
- 10. 使用android時設置圖片的一面:drawable ____在按鈕中
- 11. 如何設置背景圖片的ClientBundle資源在UiBinder的(GWT)
- 12. 如何在按鈕的中心設置圖片和文字
- 13. 網頁按鈕點擊圖片更新
- 14. 如何在按鈕中設置文本中的圖像 - android?
- 15. android-如何在列表視圖中設置按鈕參數
- 16. 如何在android中設置UIControlStateSelected的按鈕圖像?
- 17. 如何設置數值並按下網頁上的按鈕?
- 18. Android圖庫視圖 - 如何設置資源?
- 19. 在Android的按鈕上設置圖像?
- 20. 如何在單擊按鈕時設置網頁?與JavaFX
- 21. 如何設置圖像資源?
- 22. Android:如何在ListView中動態加載資源圖片?
- 23. 在android設置圖像資源中的圖像
- 24. 將圖片設置爲按鈕
- 25. 網頁打印按鈕和設置
- 26. android:如何添加圖片按鈕?
- 27. 如何在CRM Dynamics中顯示網頁/網頁資源?
- 28. 圖片資源?
- 29. 使用Vaadin在按鈕圖片/圖標中設置標題
- 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