我需要只使用文件名来显示图像,而不是使用资源id。

ImageView imgView = new ImageView(this);
imgView.setBackgroundResource(R.drawable.img1);

我在可绘制文件夹中有图像img1。我想展示文件中的图像。

我该怎么做呢?


当前回答

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

}

其他回答

String path = Environment.getExternalStorageDirectory()+ "/Images/test.jpg";

File imgFile = new File(path);
if(imgFile.exists())
{
   Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
   ImageView imageView=(ImageView)findViewById(R.id.imageView);
  imageView.setImageBitmap(myBitmap);
}

所有的答案都过时了。毕加索的作品最好是用于这种目的。它有很多功能,包括背景图像处理。

我有没有提过它非常容易使用:

Picasso.with(context).load(new File(...)).into(imageView);

你还可以使用:



    File imgFile = new  File(“filepath”);
    if(imgFile.exists())
    {
        ImageView myImage = new ImageView(this);
        myImage.setImageURI(Uri.fromFile(imgFile));

    }

这为您隐式地完成位图解码。

来自官方网站:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

ImageView image = (ImageView) findViewById(R.id.imagePreview);           
try {
    image.setImageBitmap(decodeSampledBitmap(picFilename));
} catch (Exception e) {
    e.printStackTrace();
}

方法如下:

    private int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    private Bitmap decodeSampledBitmap(String pathName,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathName, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(pathName, options);
    }

//I added this to have a good approximation of the screen size: 
    private Bitmap decodeSampledBitmap(String pathName) {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        return decodeSampledBitmap(pathName, width, height);
    }   

大多数答案都是有效的,但没有人提到高分辨率的图像会降低应用程序的速度,在我的情况下,我使用了imagesrecyclerview,它在30张图像中占用了0.9 GB的设备内存。

我/编舞:跳过73帧!应用程序可能也在这样做 它的主线有很多工作要做。

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

但我用简单的方式,Glide处理剩下的工作

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