从文件路径显示图像视图?

348
我需要仅通过文件名显示图片,而不是使用资源id。
ImageView imgView = new ImageView(this);
imgView.setBackgroundResource(R.drawable.img1);

我有一个位于drawable文件夹中的图片img1,想要在文件中显示它。

我应该如何操作?

15个回答

3
大多数答案都是有效的,但没有人提到高分辨率图像会使应用程序变慢,在我的情况下,我使用了RecyclerView来显示图片,仅30张图片就占用了0.9GB的设备内存。

I/Choreographer: Skipped 73 frames! The application may be doing too much work on its main thread.

解决方法很简单,你可以像这样降低质量:减小位图的分辨率

但我使用了更简单的方式,Glide会处理剩下的工作。

fanContext?.let {
            Glide.with(it)
                .load(Uri.fromFile(File(item.filePath)))
                .into(viewHolder.imagePreview)
        }

3

图片已完全加载

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");
        }
    });
}

2
您可以使用位图轻松完成此操作。 您可以在下面找到代码:
ImageView imageView=findViewById(R.id.imageView);

Bitmap bitMapImage= BitmapFactory.decodeFile("write path of your image");

imageView.setImageBitmap(bitMapImage);

2
private void showImage(ImageView img, String absolutePath) {
  
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    Bitmap bitmapPicture = BitmapFactory.decodeFile(absolutePath);
    img.setImageBitmap(bitmapPicture);

}

-6
mageView.setImageResource(R.id.img);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接