How do I add R drawable android?

2019-08-15 06:14发布

问题:

Basicly my question is that how do I add a drawable resource to the R.java file in android eclipse so I can use it with this code:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(),     R.drawable.kapschlogo);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
        Image myImg = Image.getInstance(stream.toByteArray());
        myImg.setAlignment(Image.MIDDLE);

        doc.add(myImg);

The image is stored in the parent project folder but I cant figure out how do I add it tho te R file.

回答1:

Since the R.java file is automatically generated from your resources you need to put the image file(s) under the res/drawable folder and from there they will be indexed and accessible from R.drawable.myImageFile(XML) or getResources().getDrawable(R.drawable.myImageFile);(java)