从URL下载图像

时间:2014-04-17 19:54:41

标签: android android-listview imagedownload google-text-to-speech

我的应用程序中有一个List View,我通过Base Adapter显示数据。他们是我面临的两个问题,并提到几个帖子,但所有建议我遵循相同的程序。

问题

  1. 我正在尝试从JSON提供的URL下载图像。一切顺利,但图像永远不会设置为Image View

  2. 我将Text to Speech绑定到Base Adapter类中的按钮的单击事件并将其释放到java类的onDestroy中但仍然在Log中出现错误,因为它说明了this和应用程序崩溃。在log erroe行中,第55行是onDestroy的第一个陈述。

  3. 这是我的代码

    Java文件

    public class DisplayWeather extends Activity {
    
        String city, date, maximumTemp, minimumTemp, description, weatherImageUrl;
        ListView weatherList;
        List <Bean> bean;
        Bitmap myBitmap, newBitmap;
        CustomBaseAdapter baseAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.display_weather);
    
            bean = new ArrayList<Bean>();
            weatherList = (ListView) findViewById(R.id.lvWeather);
    
            for(int i=0; i<WeatherHome.arrayList.size(); i++)
            {
                .
                        .
    
            }
    
    
            weatherList.setAdapter(new CustomBaseAdapter(this, bean));
        }
    
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            if (baseAdapter.tts != null)
            {
                baseAdapter.tts.stop();
                baseAdapter.tts.shutdown();
            }
            super.onDestroy();
        }
    

    基础适配器类

    public class CustomBaseAdapter extends BaseAdapter implements OnInitListener {
        Context context;
        List<Bean> bean;
        ImageView weatherImage;
        TextView weatherDate, weatherCity, weatherMinimum, weatherMaximum, weatherDescription;
        Button buttonSpeak;
        String citySpeak, dateSpeak, descriptionSpeak, maximumSpeak, minimumSpeak, weatherURL;
        TextToSpeech tts;
        Bean userBean;
        Bitmap myBitmap;
    
        public CustomBaseAdapter(Context context, List<Bean> bean) {
            // TODO Auto-generated constructor stub
            this.context = context;
            this.bean = bean;
            tts = new TextToSpeech(context, null);
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return bean.size();
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return bean.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return bean.indexOf(getItem(position));
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    
            if(convertView == null)
            {
                convertView = inflater.inflate(R.layout.custom_base_adapter, null);
                weatherImage = (ImageView) convertView.findViewById(R.id.displayImage);
    convertView.findViewById(R.id.displayDate);
                buttonSpeak = (Button) convertView.findViewById(R.id.Speak);
    
    
            }
    
            weatherURL = userBean.getImageUrl();
    
            new ImageDownload().execute();
    
            Log.i("Executing Rest Line>>>", "Skippedddddd");
            buttonSpeak.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                String cityName = weatherCity.getText().toString(); 
                String dateValue = weatherDate.getText().toString();
                String maximumValue = weatherMaximum.getText().toString();
                String minimumValue = weatherMinimum.getText().toString();
                String descriptionValue = weatherDescription.getText().toString();
    
                citySpeak = "Temprature for city "+cityName+"";
                dateSpeak = " on Date "+dateValue+"";
                maximumSpeak = "will be Maximum upto "+maximumValue+" degree ";
                minimumSpeak = " and Minimum upto"+minimumValue+" degree ";
                descriptionSpeak = "and The atmosphere seems to be "+descriptionValue+"";
    
                speakTempratureValues();
                }
            });
    
            return convertView;
        }
    
        private class ImageDownload extends AsyncTask<String, Void, Bitmap>{
    
            protected Bitmap doInBackground(String... arg0){
    
                try{
                    Log.e("src",weatherURL);
                    URL url = new URL(weatherURL);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    InputStream input = connection.getInputStream();
                    myBitmap = BitmapFactory.decodeStream(input);       
                    Log.e("Bitmap","returned");
                    return myBitmap;
                }
                catch(Exception e){
                    e.printStackTrace();
                    return null;
                }
    
            }
    
            protected void onPostExecute(Bitmap result){
                 if(result!=null)
                 {
                    Log.i("OnPost>>>", ""+result);
                    weatherImage.setImageBitmap(result);
                 }
    
            }
        }
    
        protected void speakTempratureValues() {
            // TODO Auto-generated method stub
            tts.setSpeechRate(-4);
            tts.speak(citySpeak, TextToSpeech.QUEUE_FLUSH, null);
            tts.speak(dateSpeak, TextToSpeech.QUEUE_ADD, null);
            tts.speak(maximumSpeak, TextToSpeech.QUEUE_ADD, null);
            tts.speak(minimumSpeak, TextToSpeech.QUEUE_ADD, null);
            tts.speak(descriptionSpeak, TextToSpeech.QUEUE_ADD, null);
            tts.speak("Thank You", TextToSpeech.QUEUE_ADD, null);
        }
    
    
        @Override
        public void onInit(int status) {
            // TODO Auto-generated method stub
            if(status==TextToSpeech.SUCCESS){
                int result = tts.setLanguage(Locale.getDefault());
    
                if (result == TextToSpeech.LANG_MISSING_DATA
                        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "This Language is not supported");
                }
                else{
    
                    speakTempratureValues();
                }
            }
            else{
                Log.e("TTS", "Initialization Failed");
            }
        }
    
    }
    

2 个答案:

答案 0 :(得分:1)

在您的AsyncTask下载完图像之前,您可能会在getView方法中返回convertView。您是否可以使用线程,并使用线程连接方法,以便您的应用程序等待下载图像?对于AsyncTask,您通常会使用进度对话框,直到任务完成,但我不认为您可以在适配器内执行此操作。

如何替换它:

new ImageDownload().execute();

用这个:

new Thread(new Runnable() {
public void run() {
  try{
            Log.e("src",weatherURL);
            URL url = new URL(weatherURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            myBitmap = BitmapFactory.decodeStream(input);       
            Log.e("Bitmap","returned");
            return myBitmap;
        }
        catch(Exception e){
            e.printStackTrace();
            return null;
        }}
}).join();

然后显然摆脱了ImageDownload类。我只是把它丢给你,没有测试它或任何东西。我认为这应该让你更接近。

答案 1 :(得分:0)

路易斯建议的是正确的方法。我相信这会奏效。它不适合你的原因并不确定,但尝试这种方式:

Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try{
                        Log.e("src",weatherURL);
                        URL url = new URL(weatherURL);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setDoInput(true);
                        connection.connect();
                        InputStream input = connection.getInputStream();
                        myBitmap = BitmapFactory.decodeStream(input);       
                        Log.e("Bitmap","returned"+myBitmap);

                        if(myBitmap!=null)
                     {
                        Log.i("OnPost>>>", ""+myBitmap);
                        weatherImage.setImageBitmap(myBitmap);
                     }
                    }
                    catch(Exception e){
                        e.printStackTrace();

                    }

                }
            };

            Thread t = new Thread(runnable);
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

可能是你缺少start方法,你可能没有调用它。 我希望这项工作。

相关问题