简体   繁体   中英

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

I was successful to load image to linear layout and image from local path as below code

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

However, I need to save the path of these image in to file. Thus, I cannot use above code to load image to imageview and linearlayout. Instead of this, I want to find the way to load image from path. Could you help me to solve it in android. This is my code to get path

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

Thanks in advance

Some ways are:

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

Other ways :

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();

You can Create a Drawable from a String path like this:

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

Thanks

Use int type to save the drawable id.

Eg.

int drawablePerson = R.drawable.imgPerson;

int drawableId = R.drawable.layoutID;

Add like this in path..

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

I think this is work for your problem.

first you have to save the data to a file using this method Save Bitmap to File

then you can use Bitmap bitmap = BitmapFactory.decodeFile(String pathName); refer to Android Developer BitmapFactory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM