Android get image path from drawable as string

前端 未结 5 592
天涯浪人
天涯浪人 2020-12-04 19:15

Is there any way that I can get the image path from drawable folder in android as String. I need this because I implement my own viewflow where I\'m showing the images by us

5条回答
  •  一生所求
    2020-12-04 20:12

    based on the some of above replies i improvised it a bit

    create this method and call it by passing your resource

    Reusable Method

    public String getURLForResource (int resourceId) {
        //use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
        return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
    }
    

    Sample call

    getURLForResource(R.drawable.personIcon)
    

    complete example of loading image

    String imageUrl = getURLForResource(R.drawable.personIcon);
    // Load image
     Glide.with(patientProfileImageView.getContext())
              .load(imageUrl)
              .into(patientProfileImageView);
    

    you can move the function getURLForResource to a Util file and make it static so it can be reused

提交回复
热议问题