画像ボタンがあり、インターネットURLから背景画像を設定したいのですが。背景画像をSDカードに保存したくないのですが、代わりに画像ボタンの画像をURLにする必要があります。Androidでそれを行うにはどうすればよいですか
質問する
9023 次
3 に答える
0
まず、イメージを Drawable としてダウンロードする必要があります: Android Drawable Images from URL
その後、それを背景のドローアブルとして設定します
button.setBackgroundDrawable(drawable)
于 2013-03-04T11:19:35.827 に答える
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;
}
于 2013-03-04T11:17:36.407 に答える
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);
}
}
于 2013-03-04T11:19:10.453 に答える