简体   繁体   English

我的应用程序正在显示图像网址。 我如何下载图像以使其可以显示在我的应用程序中

[英]My app is displaying the image url. How do i download the image so that it can display on my app

//my adapter. //我的适配器。 I'm trying to find a way to implement Picasso in the code below or the activity for it to display the image. 我正在尝试找到一种在下面的代码或显示图片的活动中实施毕加索的方法。 I'm also using a gridview for displaying the images. 我还使用gridview来显示图像。 The getImage() below is the method responsible for getting the url 下面的getImage()是负责获取url的方法

        public CategoryViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            mContext = itemView.getContext();
        }

        public void bindCategory(Category category) {
            mNameTextView.setText(category.getImage());
        }
    }
}

You can get the InputStream from the url, and decode a bitmap to display it in a ImageView . 您可以从网址获取InputStream ,并解码位图以在ImageView显示它。

try {
    URL url = new URL(getImage());
    Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
    imageView.setImageBitmap(bitmap);
} catch (IOException e) {
    e.printStackTrace();
}

I recommend you to use Picasso library, using which you can easily load images using following code: 我建议您使用Picasso库,通过该库,您可以使用以下代码轻松加载图像:

Picasso.with(this/*context*/).load(getImage()).into(imageView);

Edit: 编辑:

As you wished to get image directly from the method, here it goes: 正如您希望直接从方法中获取图像一样,它如下:

public Bitmap getImage(){
    String imageUrl  = "https://farm"+getFarm()+".staticflickr.com/"+getServer()
        +"/"+getId()+"_"+getSecret()+"_m.jpg";
    try {
        URL url = new URL(imageUrl);
        return BitmapFactory.decodeStream(url.openStream());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

You can open the Image in a Webview. 您可以在Web视图中打开图像。 Make sure you have a WebView component in your Activity Xml also Declare the WebView variable outside of your Class ( Global variable ) So that u can use it inside a function. 确保在Activity Xml中有一个WebView组件,并且还在类(全局变量)之外声明WebView变量,以便您可以在函数内使用它。

public void getImage(){
        String imageUrl  = "https://farm"+getFarm()+".staticflickr.com/"+getServer()+"/"+getId()+"_"+getSecret()+"_m.jpg";
        mWebView.loadDataWithBaseURL(null, "<html><head></head><body><table style=\"width:100%; height:100%;\"><tr><td style=\"vertical-align:middle;\"><img src=\"" + imageUrl + "\"></td></tr></table></body></html>", "html/css", "utf-8", null);

    }

You can view the image like that , using a custom lib 您可以使用自定义库查看类似的图像

Picasso.with(this).load(getImage()).into(imageView);

or 要么

Glide.with(this).load(getImage()).into(imageView);

and downloading the image: 并下载图片:

public void DownloadImageFromPath(String path){
            InputStream in =null;
            Bitmap bmp=null;
             ImageView iv = (ImageView)findViewById(R.id.img1);
             int responseCode = -1;
            try{

                 URL url = new URL(path);//"http://192.xx.xx.xx/mypath/img1.jpg
                 HttpURLConnection con = (HttpURLConnection)url.openConnection();
                 con.setDoInput(true);
                 con.connect();
                 responseCode = con.getResponseCode();
                 if(responseCode == HttpURLConnection.HTTP_OK)
                 {
                     //download 
                     in = con.getInputStream();
                     bmp = BitmapFactory.decodeStream(in);
                     in.close();
                     iv.setImageBitmap(bmp);
                 }

            }
            catch(Exception ex){
                Log.e("Exception",ex.toString());
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何追踪哪个图像是我的应用程序的杀手? - How do i trace which image is the killer of my app? 如何正确地将图像下载到我的相机胶卷? - How do I properly download an image to my camera roll? 如何开始将图像字符串文件转换为字节数组,以便将其存储在数据库中? - How do i start converting my image string file into a byte array so I can store it in my data base? 如何同时显示我的图像和按钮? - How do I get both my image and button to display? 如何使用以前在Spring Boot应用程序外部文件夹中上传的图像 - How can I use an image that I previously uploaded in a folder outside my spring boot app 如何用图像中的蓝色半透明替换我的Android应用程序谷歌地图半径圆圈? - How do I replace my android app Google maps radius circle with a blue translucent one as in image? 如何在我的 Web App 上显示用户总数? - How do I display the total number of users on my Web App? 如何在我的应用程序而不是网络浏览器中打开 URL? - How do I open a URL in my app instead of the web browser? 我如何知道机器上Web应用程序的URL? - How do I know the URL of a web app on my machine? 如何将我的 android 应用程序的重定向 URL 列入白名单 - How do i whitelist a redirect URL for my android app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM