Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

137.5. android.os.NetworkOnMainThreadException

网络连接会阻塞 UI,必须在线程中异步访问网络

		
		aigcSpeech.resetSpeech();
        aigcSpeech.Player().play(picture.audio, binding.progressBar);
        getActivity().runOnUiThread(() -> {
            binding.textView.setText(picture.story);
            new Thread(() -> {
                try {

                    InputStream inputStream = new URL(picture.image).openStream();
                    Drawable drawable = Drawable.createFromStream(inputStream, null);
                    binding.imageView.setImageDrawable(drawable);
                } catch (Exception e) {
                    Log.e(TAG, e.toString());
                    e.printStackTrace();
                }
            }).start();

         });