Convert a File Object to Bitmap
Asked Answered
A

6

64

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.

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

Angio answered 4/10, 2013 at 2:18 Comment(2)
use bitmapfactory to create bitmap from fileKiushu
@PulkitSethi Can you show me how to do that, not sure of this. most examples uses path string of the image. In my case I am using the file itself.Angio
A
182

You should be able to use BitmapFactory:

File mSaveBit; // Your image file
String filePath = mSaveBit.getPath();  
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
Appose answered 4/10, 2013 at 2:41 Comment(4)
I got error: The method decodeFile(String) in the type BitmapFactory is not applicable for the arguments (File)Angio
Updated my answer, sorry, missed the getPath() method.Appose
@Appose Please add description when answering questions so that everybody can understand your answer, i.e. here, what is type of filePath, mSaveBit, it can be known from the question but still its a good habbit, well good answer (upvoted).Duda
returning null in android 10Leander
G
17
  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);
    
Glengarry answered 5/6, 2015 at 5:45 Comment(0)
D
2

You can use this function to get Bitmap from file path

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
}
Dracula answered 15/1, 2021 at 21:55 Comment(0)
J
0

Here is a simple code to create a scaled image for ImageView in this case - Width:400 - Height: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));
Junket answered 13/9, 2017 at 18:17 Comment(0)
B
0

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)
                }
            }
        }
Bybee answered 11/10, 2018 at 2:53 Comment(0)
M
0

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 .

Just use:

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

Maisonette answered 27/8, 2019 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.