Show Image View from file path?

前端 未结 13 920
眼角桃花
眼角桃花 2020-11-22 11:18

I need to show an image by using the file name only, not from the resource id.

ImageView imgView = new ImageView(this);
imgView.setBackgroundResource(R.drawa         


        
13条回答
  •  孤城傲影
    2020-11-22 11:39

    onLoadImage Full load

    private void onLoadImage(final String imagePath) {
        ImageSize targetSize = new ImageSize(imageView.getWidth(), imageView.getHeight()); // result Bitmap will be fit to this size
    
        //ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleto
        com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(getContext()));
    
        imageLoader.loadImage(imagePath, targetSize, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(final String imageUri, View view) {
                super.onLoadingStarted(imageUri, view);
    
                progress2.setVisibility(View.VISIBLE);
    
                new Handler().post(new Runnable() {
                    public void run() {
                        progress2.setColorSchemeResources(android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
    
                        // Picasso.with(getContext()).load(imagePath).into(imageView);
                        // Picasso.with(getContext()).load(imagePath) .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).into(imageView);
    
                        Glide.with(getContext())
                                .load(imagePath)
                                .asBitmap()
                                .into(imageView);
                    }
              });
            }
    
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if (view == null) {
                    progress2.setVisibility(View.INVISIBLE);
                }
                // else { 
                  Log.e("onLoadImage", "onLoadingComplete");
                //   progress2.setVisibility(View.INVISIBLE);
                // }
                // setLoagingCompileImage();
            }
    
            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                super.onLoadingFailed(imageUri, view, failReason);
                if (view == null) {
                    progress2.setVisibility(View.INVISIBLE);
                }
                Log.e("onLoadingFailed", imageUri);
                Log.e("onLoadingFailed", failReason.toString());
            }
    
            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                super.onLoadingCancelled(imageUri, view);
                if (view == null) {
                    progress2.setVisibility(View.INVISIBLE);
                }
                Log.e("onLoadImage", "onLoadingCancelled");
            }
        });
    }
    

提交回复
热议问题