【问题标题】:Convert a File Object to Bitmap将文件对象转换为位图
【发布时间】:2013-10-10 21:59:53
【问题描述】:

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

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

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

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

【问题讨论】:

  • 使用bitmapfactory从文件创建位图
  • @PulkitSethi 你能告诉我怎么做吗,不确定。大多数示例使用图像的路径字符串。就我而言,我使用的是文件本身。

标签: java android caching bitmap universal-image-loader


【解决方案1】:

你应该可以使用BitmapFactory:

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

【讨论】:

  • 我收到错误:BitmapFactory 类型中的方法 decodeFile(String) 不适用于参数(文件)
  • 更新了我的答案,抱歉,错过了getPath()方法。
  • @Karl 请在回答问题时加上描述,让大家明白你的答案,即这里filePathmSaveBit的类型,从问题中可以知道但仍然是一个好习惯,好答案(赞成)。
  • 在 android 10 中返回 null
【解决方案2】:
  1. 定义文件

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

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

    myImageView.setImageBitmap(bitmap);
    

【讨论】:

    【解决方案3】:

    您可以使用此功能从文件路径获取位图

    fun getBitmap(filePath:String):Bitmap?{
        var bitmap:Bitmap?=null
        try{
            var f:File = File(path)
            var options = BitmapFactory.Options()
            options.inPreferredConfig = Bitmap.Config.ARGB_8888
            bitmap = BitmapFactory.decodeStream(FileInputStream(f),null,options)
        }catch (e:Exception){
    
        }
        return bitmap
    }
    

    【讨论】:

      【解决方案4】:

      在这种情况下,这是一个为 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));
      

      【讨论】:

        【解决方案5】:

        Kotlin 版本

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

        【讨论】:

          【解决方案6】:

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

          只需使用:

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

          【讨论】:

            猜你喜欢
            • 2017-06-07
            • 2010-12-04
            • 2015-06-28
            • 1970-01-01
            • 1970-01-01
            • 2011-06-19
            • 1970-01-01
            • 2017-07-15
            • 1970-01-01
            相关资源
            最近更新 更多