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

发布于 2024-12-20 13:02:41 字数 179 浏览 3 评论 0原文

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

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

I have a textView. In my code I am adding some lines of text in it. I also want to display some image from an external URL (not from my resource folder) just in between those lines. Every thing is dynamic i.e. the text generated and the image URL will be generated on the flow.So i have to fetch the image through my code and add it.

Wondering if there is a way to insert images from external URL within text view? Also any better approach is always welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戴着白色围巾的女孩 2024-12-27 13:02:41

您必须将其与 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);
        } 

希望有帮助

You will have to use this along with asynctask,
open connection in doInbackground()
set image to textview in onPostExecute()

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

hope it helps

一腔孤↑勇 2024-12-27 13:02:41

您可能想要使用异步任务来获取图像。这将在您的其他任务的后台运行。您的代码可能如下所示:

    public class ImageDownloader extends AsyncTask<String, Integer, Bitmap>{

    private String url;
    private final WeakReference<ImageView> imageViewReference;

    //a reference to your imageview that you are going to load the image to
    public ImageDownloader(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... arg0) {
        if(isCancelled())
            return null;
        Bitmap retVal;

        url = arg0[0];//this is the url for the desired image
        ...download your image here using httpclient or another networking protocol..
        return retVal;
    }
    @Override
    protected void onPostExecute(Bitmap result) {
        if (isCancelled()) {
            result = null;
            return;
        }
        ImageView imageView = imageViewReference.get();
        imageView.setImageBitmap(result);
    }
    @Override
    protected void onPreExecute() {
        ...do any preloading you might need, loading animation, etc...
    }

You are going to probably want to use an asynctask to grab the image. This will run in the background from your other tasks. Your code may look something like this:

    public class ImageDownloader extends AsyncTask<String, Integer, Bitmap>{

    private String url;
    private final WeakReference<ImageView> imageViewReference;

    //a reference to your imageview that you are going to load the image to
    public ImageDownloader(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... arg0) {
        if(isCancelled())
            return null;
        Bitmap retVal;

        url = arg0[0];//this is the url for the desired image
        ...download your image here using httpclient or another networking protocol..
        return retVal;
    }
    @Override
    protected void onPostExecute(Bitmap result) {
        if (isCancelled()) {
            result = null;
            return;
        }
        ImageView imageView = imageViewReference.get();
        imageView.setImageBitmap(result);
    }
    @Override
    protected void onPreExecute() {
        ...do any preloading you might need, loading animation, etc...
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文