【问题标题】:Convert drawable picture in Bitmap Android在Bitmap Android中转换可绘制图片
【发布时间】:2016-12-06 13:42:36
【问题描述】:

如何在 Bitmap 中转换我拥有的 String 形式的可绘制资源,如下所示

android.resource://com.example.myapp/drawable/name_picture

【问题讨论】:

  • 我刚刚在google中使用了你的问题标题:stackoverflow.com/questions/8717333/…
  • 这不是我问的。我想将它从我指定的字符串转换为位图,而不仅仅是转换图片

标签: android bitmap type-conversion android-drawable android-bitmap


【解决方案1】:
    Uri uriString = Uri.parse("android.resource://com.example.myapp/drawable/name_picture
");
                String lastPathIcon = uriString.getLastPathSegment();
                int drawableResource = getApplicationContext().getResources()
                        .getIdentifier("drawable/" + lastPathIcon, null, getApplicationContext().getPackageName());

                largeIcon = BitmapFactory.decodeResource(getResources(), drawableResource);

这是对我有用的解决方案。

【讨论】:

    【解决方案2】:
    Try the following(there maybe typos):
    
    String path = "android.resource://com.example.myapp/drawable/name_picture";
    String[] splitByPath = path.split("drawable/");
    if(splitByPath!=null && splitByPath.length >1){
        int varId = getResources().getIdentifier(splitByPath[1], "drawable", getPackageName());
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),varId);
    }else{
        //Improper path
    }
    

    【讨论】:

      【解决方案3】:
      Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(),R.drawable.myphoto);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-25
        相关资源
        最近更新 更多