在 Bitmap 中转换可绘制图片 Android

Convert drawable picture in Bitmap Android

如何在位图中转换我拥有的字符串形式的可拖资源,如下所示

android.resource://com.example.myapp/drawable/name_picture
Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(),R.drawable.myphoto);
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
}
    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);

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