【问题标题】:Optimise android loading url from web database优化 android 从 web 数据库加载 url
【发布时间】:2012-12-31 10:33:15
【问题描述】:

在我的应用程序中,我从我的数据库中加载了一些 URL,并将它们存储在 ArrayList<Bitmap> 中,以便将它们放入图库中。它工作得很好,但速度很慢。问题是我在显示图像之前加载了ArrayList 中的所有位图。

        try
        {
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++)
            {
                Temp = jArray.get(i).toString();

                HttpURLConnection connection = null;
                try 
                {
                    connection = (HttpURLConnection) new URL(Temp).openConnection();
                } 
                catch (MalformedURLException e) 
                {
                    e.printStackTrace();
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
                connection.connect();
                InputStream input = connection.getInputStream();

                x = BitmapFactory.decodeStream(input);
                bitmapArray.add(x);
            }
        }
        return bitmapArray; // returning the array list with all the bitmaps inside

    protected void onPostExecute(ArrayList<Bitmap> donnees)
    {
        SetupInterface(); // This method sends the arraylist to an image adatper and display bitmaps to the screen.
    }

我的问题是 SetupInterface() 仅在数组已满并返回时才被调用。所以当我打开我的活动时,显示画廊大约需要 4-5 秒,因为在显示它们之前必须加载数组中的所有位图。怎么做更快?

【问题讨论】:

    标签: android bitmap android-asynctask gallery


    【解决方案1】:

    下载图像和解码图像需要时间。您已经知道 jArray 的内容(至少是这些图像的长度、名称......)。您应该将带有默认背景图像的数组直接发送到图像适配器,并使用 AsyncTask 一张一张地加载这些实际图像。

    【讨论】:

      【解决方案2】:

      在活动开始时加载并仅显示第一张图片。在显示您的活动并完成所有初始化工作后,您将加载其余图像。你可以:

      1. 在后台线程中一一加载它们,例如使用Handler 或AsyncTask。第二个会更容易实施。
      2. 加载下一张图片,当它必须加载时,例如用户点击“下一张”。它被称为延迟加载。

      【讨论】:

      • 你能给我一个一个一个加载位图并将它们发送到 ImageAdapter 的例子吗?我通过在 postexecute 中创建一个新的 asynctask 并一次又一次地调用我的方法直到数组已满来尝试它......这可能不是最好的方法......
      猜你喜欢
      • 2012-09-07
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      相关资源
      最近更新 更多