位图在文本后不显示图像的问题

时间:2011-02-09 03:21:05

标签: android

我正在尝试直接在我从SD卡中的文件中检索到的文本下显示图像,但只显示文本但不显示图像,

已经尝试了几种方法,但不是可行的

下面是我显示它们的代码

//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.drawBitmap(bitmap, 130, 10, null);

2 个答案:

答案 0 :(得分:0)

好吧,我没有在您的代码中看到您尝试设置位图的任何位置。无论如何,使用化合物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));

答案 1 :(得分:0)

执行此操作时:

Canvas canvas = new Canvas(bitmap);

您正在创建一个Canvas来将绘制到位图中。这不是你想要的方式。相反,只需告诉文本视图使用您的图标作为顶部drawable:

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