Show Image View from file path?

前端 未结 13 1034
眼角桃花
眼角桃花 2020-11-22 11:18

I need to show an image by using the file name only, not from the resource id.

ImageView imgView = new ImageView(this);
imgView.setBackgroundResource(R.drawa         


        
13条回答
  •  青春惊慌失措
    2020-11-22 11:33

           public static Bitmap decodeFile(String path) {
        Bitmap b = null;
        File f = new File(path);
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
    
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
            BitmapFactory.decodeStream(fis, null, o);
            fis.close();
    
            int IMAGE_MAX_SIZE = 1024; // maximum dimension limit
            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }
    
            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
    
            fis = new FileInputStream(f);
            b = BitmapFactory.decodeStream(fis, null, o2);
            fis.close();
    
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return b;
    }
    
    public static Bitmap showBitmapFromFile(String file_path)
    {
        try {
            File imgFile = new  File(file_path);
            if(imgFile.exists()){
    
                Bitmap pic_Bitmap = decodeFile(file_path);
                return pic_Bitmap;
    
            }
        } catch (Exception e) {
            MyLog.e("Exception showBitmapFromFile");
            return null;
        }
        return null;
    }   
    

    if you are using image loading in List view then use Aquery concept .

    https://github.com/AshishPsaini/AqueryExample

         AQuery  aq= new AQuery((Activity) activity, convertView);
                //load image from file, down sample to target width of 250 pixels .gi 
        File file=new File("//pic/path/here/aaaa.jpg");
        if(aq!=null)
        aq.id(holder.pic_imageview).image(file, 250);
    

提交回复
热议问题