当前位置: 代码迷 >> 综合 >> android studio 线程安全Only the original thread that created a view hierarchy can touch its views
  详细解决方案

android studio 线程安全Only the original thread that created a view hierarchy can touch its views

热度:71   发布时间:2024-01-14 12:19:03.0

在加载bitmap时遇到系统线程报错,通过参考https://developer.android.com/guide/components/processes-and-threads.html#Processes

解决了问题,代码:

public static void setPic(final ImageView ivPic, final String pic_url){new Thread(new Runnable() {@Override
        public void run() {try {HttpURLConnection urlConnection=(HttpURLConnection)new URL(pic_url).openConnection();urlConnection.connect();InputStream is=urlConnection.getInputStream();final Bitmap bitmap= BitmapFactory.decodeStream(is);ivPic.post(new Runnable() {@Override
                    public void run() {ivPic.setImageBitmap(bitmap);}});is.close();} catch (IOException e) {e.printStackTrace();}}}).start();

  相关解决方案