重複の可能性:
画像ビューに画像が表示されない
サーバーからダウンロード中にAndroidで画像を表示する方法誰でもこれに関するコードを共有できますか
Drawable drawable = LoadImage(ImageURL);
PhotoImageView.setImageDrawable(drawable);
//PhotoImageView is the ImageView in which you want to load your image from server
public Drawable LoadImage(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch (Exception e)
{
System.out.println("Exc=" + e);
return null;
}
}
Please Use below code for get image from url and display into imageview.
public class image extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
RelativeLayout mRlayout1 = (RelativeLayout) findViewById(R.id.mRlayout1);
Drawable d=new BitmapDrawable(bitmap);
mRlayoutLogin.setBackgroundDrawable(d);
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
}
通常、AsycTask を使用してダウンロード中に ProgressBar が回転していることを示します。
public class Downloader extends AsyncTask<String,Integer,Bitmap> {
@Override
protected void onPreExecute() {
// show spinning progress bar
}
@Override
protected Bitmap doInBackground(String... arg0) {
// download the image
return null;
}
@Override
protected void onPostExecute(Bitmap downloadedImage) {
// hide spinning progress bar
// show downloaded image
}
}
これを試してみてください。
class DownloadImage extends AsyncTask<Void, Void, Void> {
private ProgressDialog bar;
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
bar.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
bar = new ProgressDialog(context);
bar.setIndeterminate(true);
bar.setTitle(context.getString(R.string.app_name));
bar.setMessage(context.getString(R.string.please_wait));
bar.show();
}
@Override
protected Void doInBackground(Void... arg0) {
downloadImage();
return null;
}
void downloadImage() {
//Put the logic for download image from url
}
}
または こちらをご確認ください。