How to install an image that is stored in the file system along the path in the database?

I have three questions:

  • If I want to save image.pngto the file system, where should I store it res/drawable/image_1.pngorres/drawable/images/image_1.png

  • And I'm going to save the image path in the database. What should I put in the image_pathex field . image_1or images/image_1or etc.

  • How can I get the image path from the database and set the image for viewing by following my code below? Could you guys change it for me?

I already have an answer

  • if the image file is stored in the file system in assets/images/pic_1.png
  • In the database, "image_path field", you put in it images/pic_1.png.
  • Get and set the image: according to Trim's answer.

And I installed the following code according to Trim's answer.

Many thanks

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

+3
source share
1 answer

You can put your images in the assets / images directory . In this case, the path you will use in getAssets().open(String path)will look like "images / pic_1.png". You can call getAssets()anywhere in your activity. There is also a method setImageBitmap(Bitmap bm). You can create a bitmap from the path through BitmapFactory.decodeFile(String path).

+5
source

All Articles