可能重复:
图像未显示在图像视图上
如何在从服务器下载图像时在 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;
}
}
请使用以下代码从 url 获取图像并显示到 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
}
}
或请在此处查看。