62

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

rahstame
  • 2,148
  • 4
  • 23
  • 53

6 Answers6

179

You should be able to use BitmapFactory:

File mSaveBit; // Your image file
String filePath = mSaveBit.getPath();  
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43
Karl
  • 3,394
  • 2
  • 22
  • 31
  • I got error: The method decodeFile(String) in the type BitmapFactory is not applicable for the arguments (File) – rahstame Oct 04 '13 at 02:43
  • 1
    Updated my answer, sorry, missed the `getPath()` method. – Karl Oct 04 '13 at 02:47
  • @Karl 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). – Ravi Vaniya May 11 '18 at 07:12
  • 1
    returning null in android 10 – Abraham Mathew Sep 09 '21 at 11:28
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);
    
King of Masses
  • 18,405
  • 4
  • 60
  • 77
Hardik Gajera
  • 1,351
  • 1
  • 14
  • 21
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
}
Munib
  • 65
  • 6
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));
Bilal Mustafa
  • 750
  • 7
  • 16
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)
                }
            }
        }
Kingsley Mitchell
  • 2,412
  • 2
  • 18
  • 25
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");

Euler Tiago
  • 134
  • 2
  • 4