如何根据数据库中的路径设置存储在文件系统中的图像视图?

Apr*_*ith 3 filesystems android image filepath

我有三个问题:

  1. 如果我选择存储image.png在文件系统中我应该在哪里存储它res/drawable/image_1.pngres/drawable/images/image_1.png

  2. 我将在数据库中存储图像路径.我应该把什么放在image_path前场.image_1images/image_1或等.

  3. 如何从数据库获取图像路径并设置图像以查看底部的代码?你能帮我换一下吗?

我已经回答了

  1. 以便将图像文件存储在文件系统中 assets/images/pic_1.png
  2. 在数据库中,"image_path字段",您将放入images/pic_1.png其中.
  3. 获取和设置图像:根据Trim的回答.

我根据Trim的回答修复了以下代码.

非常感谢

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

            }
          });


    }

}
Run Code Online (Sandbox Code Playgroud)

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();
        }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*çin 5

您可以将图像放在"assets/images"目录下.在这种情况下,您将使用的路径getAssets().open(String path)将类似于"images/pic_1.png". 您可以getAssets()在活动的任何地方打电话.还有一种setImageBitmap(Bitmap bm)方法.您可以从路径创建位图BitmapFactory.decodeFile(String path).