如何在从服务器下载图像时在Android中显示图像 请允许任何人分享我关于此的代码
答案 0 :(得分:1)
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;
}
}
答案 1 :(得分:0)
请使用以下代码从网址获取图片并显示到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;
}
}
答案 2 :(得分:0)
通常在使用AsyncTask下载时显示旋转进度条:
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
}
}
答案 3 :(得分:0)
试试这可能对你有帮助。
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
}
}
或请检查here.