Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图像视图未显示使用线程的图像_Java_Android_Multithreading_Bitmap - Fatal编程技术网

Java 图像视图未显示使用线程的图像

Java 图像视图未显示使用线程的图像,java,android,multithreading,bitmap,Java,Android,Multithreading,Bitmap,我使用这段代码来显示图像,因为我在没有线程的情况下运行它,它可以正常工作,但是使用线程,我只获得图像视图,其中没有任何图片。对于我来说,在程序的这一部分使用线程非常重要,因为它非常慢:( 这是我的密码: @Override public View getView(final int position, View convertView, ViewGroup parent) { Log.e("sara" , "this part takes

我使用这段代码来显示图像,因为我在没有线程的情况下运行它,它可以正常工作,但是使用线程,我只获得图像视图,其中没有任何图片。对于我来说,在程序的这一部分使用线程非常重要,因为它非常慢:( 这是我的密码:

        @Override
        public View getView(final int position,  View convertView, ViewGroup parent) {
            Log.e("sara" , "this part takes time");


            LayoutInflater inflater = getLayoutInflater();

            convertView = getLayoutInflater().inflate(R.layout.gallery_gridsq, parent, false);
            iv = (ImageView) convertView.findViewById(R.id.icon);
          final File file = new File(Uri.parse(getItem(position).toString()).getPath());
            //File file = new File("/text.jpg");

            Runnable runnable = new Runnable() {
                @Override
                public void run() {

                    Bitmap bmp = null;
                    BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            try {
                BitmapFactory.decodeStream(new FileInputStream(file), null, options);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            options.inJustDecodeBounds = false;
            options.inSampleSize = 2;
            try {
                bmp = BitmapFactory.decodeStream(new FileInputStream(file), null, options);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        iv.setImageBitmap(bmp);

                }
            };
            new Thread(runnable).start();
            return convertView;
        }
    }

使用
AsyncTask

文件:

AsyncTask允许正确且轻松地使用UI线程 允许您在上执行后台操作和发布结果 UI线程,而无需操作线程和/或处理程序


doInBackground()
中执行繁重的操作,并在
onPostExecute()中将图像设置为imageView

如果使用像GLIDE()这样的图像加载库会更好,它将处理所有图像加载问题。