如何设置根据数据库路径,存储在文件系统映像的看法? [英] How can I set image view which stored in file system according to path in database?
问题描述
我有三个问题:
-
如果我选择存储
image.png
在文件系统中我应该在哪里存放RES /绘制/ image_1.png
或RES /绘制/图像/ image_1.png
-
和我将存储在数据库中的图像的路径。我应该放在
image_path
字段前。IMAGE_1
或图片/ IMAGE_1
或等。 -
我怎样才能获得的图像从数据库路径,并设置图片查看按照我的code在底部?难道你们改变了我?
我已经有答案
- 在的情况下存储在文件系统映像文件中的
资产/图像/ pic_1.png
- 在数据库中,image_path场,你会把它
图片/ pic_1.png
。 - 要获取和设置图像:根据微调的答案
和我照着裁剪的答案确定以下code。
非常感谢
placeListActivity.class
公共类placeListActivity扩展ListActivity {
私有静态的MyDB mDbHelper;
的String []从=新的String [] {Constants.COL_TITLE};
INT []到=新INT [] {R.id.list_place_title};
私人光标C;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
mDbHelper =新的MyDB(本);
mDbHelper.createDatabase();
mDbHelper.open();
C = mDbHelper.getAllPlaces();
setListAdapter(新SimpleCursorAdapter(这一点,
R.layout.list_place,C,
从到));
最终的ListView LV = getListView();
lv.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
意图I =新的意图(view.getContext(),Place.class);
i.putExtra(Constants.KEY_ID,ID);
i.putExtra(Constants.COL_TITLE,c.getString(
c.getColumnIndexOrThrow(Constants.COL_TITLE)));
i.putExtra(Constants.COL_CONTENT,c.getString(
c.getColumnIndexOrThrow(Constants.COL_CONTENT)));
i.putExtra(Constants.COL_IMAGE,c.getString(
c.getColumnIndexOrThrow(Constants.COL_IMAGE)));
startActivity(ⅰ);
}
});
}
}
Place.class
公共类将延伸活动{
私人TextView的称号;
私人TextView的内容;
私人ImageView的placeImage;
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.detail_place);
标题=(TextView中)findViewById(R.id.place_title);
内容=(TextView中)findViewById(R.id.place_content);
placeImage =(ImageView的)findViewById(R.id.place_image);
捆绑额外= getIntent()getExtras()。
如果(临时演员!= NULL){
//参考XML定义的意见,我们将触摸code
字符串stringTitle = extras.getString(Constants.COL_TITLE);
字符串的StringContent = extras.getString(Constants.COL_CONTENT);
字符串imageID = extras.getString(Constants.COL_IMAGE);
如果(标题!= NULL){
title.setText(stringTitle);
}
如果(内容!= NULL){
content.setText(的StringContent);
}
/ *如果(placeImage!= NULL){
placeImage.setImageDrawable(Drawable.createFromPath(imageID));
} * /
如果(placeImage!= NULL){
尝试 {
InputStream的路径= getAssets()开(的ImagePath)。
位图位= BitmapFactory.de codeStream(路径);
placeImage.setImageBitmap(位);
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
}
}
您可以把你的图片在的资产/图像目录。在这种情况下,您将在 getAssets()。打开(字符串路径)
会像图像/ pic_1.png。
您可以拨打 getAssets()
在活动的任何地方。
还有一个 setImageBitmap(位图BM)
方法。
您可以通过 BitmapFactory.de codeFILE创建路径位图(字符串路径)
。
I have three questions:
If I choose to store
image.png
in file system where should I store itres/drawable/image_1.png
orres/drawable/images/image_1.png
And I'm going to store path of image in database. What should I put in
image_path
field ex.image_1
orimages/image_1
or etc.How can I get path of image from database and set image to view follow my code in the bottom? Could you guys change it for me?
I already have answer
- in case storing image file in file system in
assets/images/pic_1.png
- In database, "image_path field" , you will put
images/pic_1.png
in it. - To get and set image: according to Trim's answer.
AND I have fixed the following code according to Trim's answer.
Thanks so much
placeListActivity.class
public class placeListActivity extends ListActivity {
private static MyDB mDbHelper;
String[] from = new String[] { Constants.COL_TITLE};
int[] to = new int[] {R.id.list_place_title};
private Cursor c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new MyDB(this);
mDbHelper.createDatabase();
mDbHelper.open();
c = mDbHelper.getAllPlaces();
setListAdapter(new SimpleCursorAdapter(this,
R.layout.list_place, c,
from, to));
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent i = new Intent(view.getContext(), Place.class);
i.putExtra(Constants.KEY_ID, id);
i.putExtra(Constants.COL_TITLE, c.getString(
c.getColumnIndexOrThrow(Constants.COL_TITLE)));
i.putExtra(Constants.COL_CONTENT, c.getString(
c.getColumnIndexOrThrow(Constants.COL_CONTENT)));
i.putExtra(Constants.COL_IMAGE, c.getString(
c.getColumnIndexOrThrow(Constants.COL_IMAGE)));
startActivity(i);
}
});
}
}
Place.class
public class Place extends Activity {
private TextView title;
private TextView content;
private ImageView placeImage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.detail_place);
title = (TextView) findViewById(R.id.place_title);
content = (TextView) findViewById(R.id.place_content);
placeImage = (ImageView) findViewById(R.id.place_image);
Bundle extras = getIntent().getExtras();
if (extras != null) {
// reference XML defined views that we will touch in code
String stringTitle = extras.getString(Constants.COL_TITLE);
String stringContent = extras.getString(Constants.COL_CONTENT);
String imageID = extras.getString(Constants.COL_IMAGE);
if (title != null) {
title.setText(stringTitle);
}
if (content != null) {
content.setText(stringContent);
}
/*if (placeImage != null) {
placeImage.setImageDrawable(Drawable.createFromPath(imageID));
}*/
if (placeImage != null) {
try {
InputStream path = getAssets().open(imagePath);
Bitmap bit = BitmapFactory.decodeStream(path);
placeImage.setImageBitmap(bit);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
You can put your images under "assets/images" directory. In this case, the path that you will use in getAssets().open(String path)
will be like "images/pic_1.png".
You can call getAssets()
anywhere in your activity.
There is also a setImageBitmap(Bitmap bm)
method.
You can create bitmap from path via BitmapFactory.decodeFile(String path)
.
这篇关于如何设置根据数据库路径,存储在文件系统映像的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!