某些图片未在Android 2.2.1上加载

时间:2014-04-06 20:59:53

标签: android bitmap android-asynctask android-imageview

我有一个应用程序,使用URL将图像加载到位图中,最近收到来自Android 2.2.1用户的电子邮件,该用户一直无法加载某些图像。我试过环顾四周,似乎无法找到不加载的图像之间的任何联系。此外,有时无法为她加载的图像会再次开始工作,就像没有错。我从来没有听说过任何更高版本的Android的这个问题,所以我认为这是2.2.1特有的问题。如果有人能向我解释出现了什么问题,并且(如果可能的话)如何修复它,我会非常感激。

以下是相关代码:

public class htmlGrabber extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
             try {
                    return getHtml();
                } 
                catch (IOException e) {
                    problem();
                    return null;
                }
        }//end of doInBackground

        @Override
        protected void onPostExecute(String result){
                Loading.fa.finish();
                shared_preferences=getSharedPreferences("shared_preferences_test", MODE_PRIVATE);
                shared_preferences_editor = shared_preferences.edit();
                shared_preferences_editor.putString("url_key", current);
                shared_preferences_editor.commit();
                TouchImageView img = (TouchImageView) findViewById(R.id.img);
                img.setImageBitmap(bitmap);
            }
        }

    public String getHtml() throws IllegalStateException, IOException{
        //gets html from the url stored in current, then parses through it and extracts the image url, then converts 
        //it into a bitmap image
        String html = "";

            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(current);
            HttpResponse response = client.execute(request);
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder str = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null)
            {
                str.append(line);
            }
            in.close();
            html = str.toString();

            /* Edited out the code that parses through the HTML looking for the image URL. The image URL is stored in the string "link"

            try {
                bitmap = BitmapFactory.decodeStream((InputStream)new URL(link).getContent());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        return html;
    }//end of getHtml


}//end of htmlGrabber

如果您需要澄清任何内容,请告诉我们!

1 个答案:

答案 0 :(得分:1)

当你尝试使用Android 2.2.1时,你也遇到了这个问题? 到底什么时候?

因为如果这个AsyncTask被活动所取代,而不是取决于此活动(如果你改变方向或其他什么,任务将被杀死并再次发射),并且可能是这种行为不起作用她的手机很好......想想缓存图片会更好......

您还可以看到这个question,它解释了Android中AsyncTask定义的更多问题,以及此定义的更改次数;所以在每个版本中它的工作方式都不一样,这是正常的。 他们在documentation说:

首次引入时,AsyncTasks在单个后台线程上串行执行。从DONUT开始,这被改为一个线程池,允许多个任务并行运行。启动HONEYCOMB,任务将恢复在单个线程上执行,以避免由并行执行引起的常见应用程序错误。如果您真的想要并行执行,可以使用THREAD_POOL_EXECUTOR使用此方法的executeOnExecutor(Executor,Params ...)版本;但是,请在那里查看有关其使用的警告。 在API级别11(Android 3.0.x,HONEYCOMB)中添加了executeOnExecutor()和THREAD_POOL_EXECUTOR。

- &GT;这意味着如果您创建两个AsyncTasks来下载两个文件,则第二个下载将在第一个下载完成后才会启动。如果您通过两台服务器进行聊天,并且第一台服务器已关闭,则在连接到第一台服务器之前,您将无法连接到第二台服务器。 (当然,除非您使用新的API11功能,但这会使您的代码与2.x不兼容。)

如果你想同时针对2.x和3.0+,这些东西变得非常棘手。

我建议使用例如universal image loader这是一个很好的,“着名的”非常简单的API,用于加载图片和自动处理缓存......

实际上,我正在使用通用图像加载器;解决方案与缓存的事实无关,而是与此Loader的实例与活动的上下文无关(其方法相关但通过缓存它们处理活动的生命周期),这意味着您的任务不会被活动的onDestroy()完全杀死并重新启动。您可以打开它的实例,并将其方法用于所有应用程序。当你想要杀死这个实例时,你可以调用它自己的Destroy。

我不能说,如果这样可以解决您的问题。我不认为你也可以在没有测试的情况下回答这个问题......但可能会解决其他问题......

我希望我能帮助......

相关问题