Java 将文件对象转换为位图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19172154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:43:25  来源:igfitidea点击:

Convert a File Object to Bitmap

javaandroidcachingbitmapuniversal-image-loader

提问by rahstame

I am using Universal-Image-Loader and there is this functionality that access the file cache of the image from sd card. But I don't know how to convert the returned file cache into bitmap. Basically I just wanted to assign the bitmap to an ImageView.

我正在使用 Universal-Image-Loader 并且有这个功能可以从 sd 卡访问图像的文件缓存。但是我不知道如何将返回的文件缓存转换为位图。基本上我只想将位图分配给 ImageView。

File mSaveBit = imageLoader.getDiscCache().get(easyPuzzle);

Log.d("#ImageValue: ", ""+mSaveBit.toString());
mImageView.setImageBitmap(mSaveBit);

Error: "The method setImageBitmap(Bitmap) in the type ImageView is not applicable for the arguments (File)"

错误:“ImageView 类型中的 setImageBitmap(Bitmap) 方法不适用于参数 (File)”

采纳答案by Karl

You should be able to use BitmapFactory:

您应该能够使用BitmapFactory

File mSaveBit; // Your image file
String filePath = mSaveBit.getPath();  
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);

回答by Hardik Gajera

  1. Define File

    String fileName = "/myImage.jpg";
    File file = new File(fileName); 
    
  2. get Bitmap of Image

    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    
  3. Set Bitmap to ImageView

    myImageView.setImageBitmap(bitmap);
    
  1. 定义文件

    String fileName = "/myImage.jpg";
    File file = new File(fileName); 
    
  2. 获取图像的位图

    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    
  3. 将位图设置为 ImageView

    myImageView.setImageBitmap(bitmap);
    

回答by Bilal Mustafa

Here is a simple code to create a scaled image for ImageView in this case - Width:400 - Height:400

这是在这种情况下为 ImageView 创建缩放图像的简单代码 - 宽度:400 - 高度:400

final File file = new File(Environment.getExternalStorageDirectory(),"b.jpg");
ImageView img = (ImageView) findViewById(R.id.imageview);
img.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()),400,400,false));

回答by Kingsley Mitchell

Kotlin Version

科特林版

  if (requestCode==PICK_IMAGE_REQUEST){
            if (data!=null){
                selectedfileUri=data.data
                if (selectedfileUri!=null && !selectedfileUri!!.path.isEmpty()){
                    val file = FileUtils.getFile(context,selectedfileUri)
                    val bitmap = BitmapFactory.decodeFile(file.path)
                    uimg!!.setImageBitmap(bitmap)
                }
            }
        }

回答by Euler Tiago

This is not the right question, but if you use flag .cacheInMemory() in ImageLoader setup you can retrive the bitmap without need of recreate at any time using BitmapFactory to safe memory usage .

这不是正确的问题,但是如果您在 ImageLoader 设置中使用标志 .cacheInMemory() ,您可以随时使用 BitmapFactory 来检索位图而无需重新创建以安全内存使用。

Just use:

只需使用:

Bitmap bitmap = ImageLoader.getInstance().getMemoryCache()·get("url as key");

Bitmap bitmap = ImageLoader.getInstance().getMemoryCache()·get("url as key");