android 如何从 Drawable 路径为 imageview 和线性布局设置图像

How to set image for imageview and linear layout from Drawable path in android

我成功地将图像加载到线性布局和本地路径中的图像,如下代码

    ImageView imagePerson=(ImageView) findViewById(R.id.imgPerson);
    LinearLayout layoutID=(LinearLayout)findViewById(R.id.layoutID);
    imagePerson.setImageResource(R.drawable.image1);
    layoutID.setBackgroundResource(R.drawable.layout1);

但是,我需要将这些图片的路径保存到文件中。因此,我不能使用上面的代码将图像加载到 imageview 和 linearlayout。而不是这个,我想找到从路径加载图像的方法。你能帮我在android中解决它吗?这是我获取路径

的代码
        String pathPerson="drawable://" + R.drawable.imgPerson;
        String pathID="drawable://" + R.drawable.layoutID;

提前致谢

使用int类型保存drawable id

例如

int drawablePerson = R.drawable.imgPerson;

int drawableId = R.drawable.layoutID;

部分方式为:

String imageUri = "drawable://" + R.drawable.image;

其他方式:

Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");

String path = path.toString();
String path = otherPath .toString();

您可以像这样从字符串路径创建 Drawable:

String pathGet = "/path/to/file/myFile.jpg"; 
Drawable d = Drawable.createFromPath(pathGet);

谢谢

在路径中这样添加..

  String pathPerson="wright your package name:drawable/" + R.drawable.imgPerson;

我认为这可以解决您的问题。

首先您必须使用此方法将数据保存到文件中Save Bitmap to File

那么你可以使用 Bitmap bitmap = BitmapFactory.decodeFile(String pathName); 参考Android Developer BitmapFactory