Create a Bitmap/Drawable from file path

后端 未结 6 1082
故里飘歌
故里飘歌 2020-11-28 06:56

I\'m trying to create a Bitmap or Drawable from existing file path.

String path = intent.getStringExtra(\"FilePath\");
BitmapFactory.Options option = new Bit         


        
6条回答
  •  离开以前
    2020-11-28 07:37

    you can't access your drawables via a path, so if you want a human readable interface with your drawables that you can build programatically.

    declare a HashMap somewhere in your class:

    private static HashMap images = null;
    
    //Then initialize it in your constructor:
    
    public myClass() {
      if (images == null) {
        images = new HashMap();
        images.put("Human1Arm", R.drawable.human_one_arm);
        // for all your images - don't worry, this is really fast and will only happen once
      }
    }
    

    Now for access -

    String drawable = "wrench";
    // fill in this value however you want, but in the end you want Human1Arm etc
    // access is fast and easy:
    Bitmap wrench = BitmapFactory.decodeResource(getResources(), images.get(drawable));
    canvas.drawColor(Color .BLACK);
    Log.d("OLOLOLO",Integer.toString(wrench.getHeight()));
    canvas.drawBitmap(wrench, left, top, null);
    

提交回复
热议问题