将web图像加载到Android时出现黑屏
当从网络上加载图像时,我会看到一个黑屏 我从网络上获取页面,将其放入画布,然后将其设置为ImageView 现在,当我加载它时,我会在图像中看到一个黑屏 活动代码:将web图像加载到Android时出现黑屏,android,imageview,android-canvas,Android,Imageview,Android Canvas,当从网络上加载图像时,我会看到一个黑屏 我从网络上获取页面,将其放入画布,然后将其设置为ImageView 现在,当我加载它时,我会在图像中看到一个黑屏 活动代码: public class ImageActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
public class ImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView i = (ImageView) findViewById(R.id.imageView1);
try {
String url = "http://www.rotoworld.com/images/headshots/NBA/1372.jpg";
Drawable image = ImageOperations(this, url);
i.setImageDrawable(image);
} catch (Exception ex) {
ex.printStackTrace();
}
i.setMinimumWidth(22);
i.setMinimumHeight(22);
i.setMaxWidth(22);
i.setMaxHeight(22);
}
private Drawable ImageOperations(Context ctx, String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
public Object fetch(String address) throws MalformedURLException, IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
}
XML:
试试这个
try
{
URL feedImage = new URL(imageUrl);
HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection();
InputStream is = conn.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
i.setImageBitmap(img);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
在android中打开活动时出现黑屏的常见原因是创建活动需要很长时间
我认为您应该将图像的获取放在异步任务或类似任务中。试试这个。它可能会对您有所帮助
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView= (ImageView)findViewById(R.id.imageView1);
new loadImageTask().execute("http://www.rotoworld.com/images/headshots/NBA/1372.jpg");
}
public static Drawable LoadImageFromWeb(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "");
return d;
}
catch (Exception e)
{
return null;
}
}
public class loadImageTask extends AsyncTask<String, Void, Void>
{
Drawable imgLoad;
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params)
{
imgLoad = LoadImageFromWeb(params[0]);
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
imageView.setVisibility(View.VISIBLE);
}
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView ImageView=(ImageView)findViewById(R.id.imageView1);
新建loadImageTask()。执行(“http://www.rotoworld.com/images/headshots/NBA/1372.jpg");
}
公共静态可绘制LoadImageFromWeb(字符串url)
{
尝试
{
InputStream为=(InputStream)新URL(URL).getContent();
Drawable d=Drawable.createFromStream(为“”);
返回d;
}
捕获(例外e)
{
返回null;
}
}
公共类loadImageTask扩展了AsyncTask
{
可抽出式imgLoad;
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(字符串…参数)
{
imgLoad=LoadImageFromWeb(参数[0]);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果)
{
super.onPostExecute(结果);
设置可见性(View.VISIBLE);
}
}
下面是它最后是如何做到的
需要使用Asynctask时,我还将html发送到web页面,以便从web检索实际的jpg
String namesTr1 = pb;
System.out.println(">>>>NAMEBEFORE"+pb);
String nameminus1 = namesTr1.replace(' ','-' );
System.out.println(">>>>NAMEAFTERREPLACe"+nameminus1);
PGCalc pgcalc = new PGCalc();
String playerhtml="http://www.rotoworld.com/player/nba/1860/"+nameminus1+"/1";
System.out.println(">>>>Before JSOUP"+playerhtml);
//Getting html image from Jspoup
try {
imagehtml = pgcalc.getimage(playerhtml);
System.out.println(">>>>>>jsoupimagehtml"+imagehtml);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Setting image in Imageview
try
{
URL feedImage = new URL(imagehtml);
HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection();
InputStream is = conn.getInputStream();
Bitmap img;
return img = BitmapFactory.decodeStream(is);
//imageview1.setImageBitmap(img);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
谢谢你的快速回复!!未工作我正在获取默认设置的图标。我在我的问题中添加了XML,谢谢您的时间!!谢谢从未听说过异步任务。有好的例子吗?@user1163234文档通常都很好。的文档包括一个如何使用它的示例。谢谢KKC!尝试运行并获得“E/AndroidRuntime(855):java.lang.RuntimeException:无法实例化活动组件信息{com.image/com.image.ImageActivity}:java.lang.NullPointerException”
String namesTr1 = pb;
System.out.println(">>>>NAMEBEFORE"+pb);
String nameminus1 = namesTr1.replace(' ','-' );
System.out.println(">>>>NAMEAFTERREPLACe"+nameminus1);
PGCalc pgcalc = new PGCalc();
String playerhtml="http://www.rotoworld.com/player/nba/1860/"+nameminus1+"/1";
System.out.println(">>>>Before JSOUP"+playerhtml);
//Getting html image from Jspoup
try {
imagehtml = pgcalc.getimage(playerhtml);
System.out.println(">>>>>>jsoupimagehtml"+imagehtml);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Setting image in Imageview
try
{
URL feedImage = new URL(imagehtml);
HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection();
InputStream is = conn.getInputStream();
Bitmap img;
return img = BitmapFactory.decodeStream(is);
//imageview1.setImageBitmap(img);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}