Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a File Object to Bitmap

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

like image 652
rahstame Avatar asked Oct 09 '22 22:10

rahstame


People also ask

Can object be converted into bitmap?

You can convert vector objects and text into bitmaps, and then use bitmap filters to apply effects. The process of converting vector objects into a bitmap is called Rasterization.

How do you create a bitmap image in Java?

To create a bitmap from a resource, you use the BitmapFactory method decodeResource(): Bitmap bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.


2 Answers

You should be able to use BitmapFactory:

File mSaveBit; // Your image file
String filePath = mSaveBit.getPath();  
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
like image 172
Karl Avatar answered Oct 19 '22 03:10

Karl


  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);
    
like image 14
Hardik Gajera Avatar answered Oct 19 '22 03:10

Hardik Gajera