gpt4 book ai didi

java - setImageResource() 问题 - 如何实现文件路径?

转载 作者:行者123 更新时间:2023-11-29 21:04:51 27 4
gpt4 key购买 nike

我无法找到如何将图片设置为 setImageResource()。目前我可以像这样从 res/drawable 轻松添加:

在 ListViewAdapter 中:

imageView = (ImageView) rowView.findViewById(R.id.listImageView); 
imageView.setImageResource((int) getItemId(position));

在 ViewAdapter 中:

public class ViewAdapter extends BaseAdapter { 
class Item {
public long id;
public String name;

public Item(long id, String name) {
this.id = id;
this.name = name;
}
}
public ViewAdapter(Context c) {
mContext = c;

mItems = new ArrayList<Item>();

mItems.add(new Item(R.drawable.mercury,"Mercury"));
mItems.add(new Item(R.drawable.mercury,"Mercury"));
mItems.add(new Item(R.drawable.mercury,"Mercury")); ...

那么,我怎样才能从路径添加图片呢?例如,说 "/storage/emulated/Photos/Italy.jpg"?同样,我可以将它们存储在 res/drawable 中并添加它们,但需要一种从文件中添加它的方法。尝试了不同的方法,但仍然找不到如何去做。

非常感谢!

最佳答案

File imgFile = new  File(“/sdcard/Images/test_image.jpg”);

if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);

}

关于java - setImageResource() 问题 - 如何实现文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24901922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com