将网页图像加载到 Android 时黑屏

发布于 2025-01-01 21:30:14 字数 1901 浏览 4 评论 0原文

从网络加载图像时,我遇到黑屏。

我从网络上获取页面,将其转换为画布,然后将其设置为 ImageView。

现在,当我加载它时,图像中出现黑屏。

活动代码:

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:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

When loading an image from the web I am getting a black screen.

I took the page from the web turned it in to a canvas and then set it to the ImageView.

Now when I load it I am getting a black screen in the image.

Activity's code:

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:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓬勃野心 2025-01-08 21:30:14

在 Android 中打开 Activity 时出现黑屏的常见原因是创建 Activity 需要很长时间。

我认为你应该将图像的获取放在 AsyncTask 或类似的任务中。

The usual reason for getting a black screen when opening an activity in android is that it takes to long to create the activity.

I think you should put the fetching of your image in a AsyncTask or similar.

长伴 2025-01-08 21:30:14

试试这个...

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();
            }

Try this...

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();
            }
错々过的事 2025-01-08 21:30:14

试试这个..它可能对你有帮助

 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);

                }

            }

Try this..it may helpful for you

 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);

                }

            }
情魔剑神 2025-01-08 21:30:14

这是最后是如何做到的...

需要使用 Asynctask 我还将 html 发送到网页以便从网络检索实际的 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;
}

Here is how did it at the end...

Needed to use the Asynctask I also sen the html to a web page in order to retrieve the actual jpg from the web

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文