开发者

problem with bitmap not displaying image after a text

i am trying to show an image directly under a text which i had retrieved from a file in the sdcard but only the text is showing but not the image,

have been trying several methods but non of it is workable

below is my code to display them

//Find the directory for the SD Card using the API
    //*Don't* hardcode "/sdcard"
    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard,"file.txt");

    //Read text from file
    StringBuilder text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
    }
    catch (IOException e) {
        //You'll need to add proper error handling here
    }

    //Find the view by its id
    TextView tv = (TextView)findViewById(R.id.TextView01);

    //Set the text
    tv.setText(text);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.BLACK);
    canvas.drawB开发者_Python百科itmap(bitmap, 130, 10, null);


Well, I don't see anywhere in your code where you are trying to set the bitmap. Whatever, what about using compounds drawables?

//Find the view by its id
TextView tv = (TextView)findViewById(R.id.TextView01);

//Set the text
tv.setText(text);

tv.setCompoundDrawablesWithIntrinsicBounds(null,
   null, null, getResources().getDrawable(R.drawable.icon));


When you do this:

Canvas canvas = new Canvas(bitmap);

you are creating a Canvas to draw into the bitmap. That's not the way to do what you want. Instead, just tell the text view to use your icon as the top drawable:

tv.setText(text);
tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.icon);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜