android - 如何在 textView 中显示来自 URL 的图像

标签 android android-imageview android-image

我有一个 TextView 。在我的代码中,我在其中添加了一些文本行。我还想在这些行之间显示来自外部 URL(而不是来 self 的资源文件夹)的一些图像。每件事都是动态的,即生成的文本和图像 URL 将在流程上生成。所以我必须通过我的代码获取图像并添加它。

想知道是否有办法在 TextView 中插入来自外部 URL 的图像?任何更好的方法也总是受欢迎的。

最佳答案

您必须将其与 asynctask 一起使用, 在 doInbackground() 中打开连接 在onPostExecute()

中将图像设置为textview
  try {
        /* Open a new URL and get the InputStream to load data from it. */
        URL aURL = new URL("ur Image URL");
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        /* Buffered is always good for a performance plus. */
        BufferedInputStream bis = new BufferedInputStream(is);
        /* Decode url-data to a bitmap. */
        Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();

        Drawable d =new BitmapDrawable(bm);
       d.setId("1");
 textview.setCompoundDrawablesWithIntrinsicBounds(0,0,1,0);// wherever u want the image relative to textview
        } catch (IOException e) {
        Log.e("DEBUGTAG", "Remote Image Exception", e);
        } 

希望对你有帮助

关于android - 如何在 textView 中显示来自 URL 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8464506/

相关文章:

php - Android - 在 GridView 中使用 PHP 显示服务器文件夹中的所有图像

java - 使布局不可点击但子元素可点击

没有 Res 变化的 Android 图像压缩

android - 如何从 Environment.DIRECTORY_PICTURE 目录中获取所有图像并显示在我的自定义图库中

java - Android Gradle 插件需要 Java 11 才能运行。您当前使用的是 Java 1.8。 react 原生

java - Android 应用程序在访问本地主机时崩溃

android-layout - 在 ImageView 的图像上设置标记

android - 应用程序正在调整大小

android - org.json.JSONObject 无法转换为 JSONArray

android - 获取 java.lang.RuntimeException : Stub when running Robolectric through Maven