android从URL加载图像的问题

发布于 2024-11-25 08:28:07 字数 502 浏览 1 评论 0原文

我正在 android 中制作一个应用程序,它从 URL 加载图标大小的图像

我尝试使用以下代码下载图像。

已从给定 URL 下载了一张标记为 default.png 的图像,但未下载另一张标记为 v_1234.jpg 的图像。我不知道出了什么问题。它只是返回 jpg 图像的 null 值。

我不确定这是 .jpg 格式的问题,我的代码没有下载 jpg 格式的图像,还是标签名称问题,由于标签中的下划线 (_) 使其无法下载。

请帮助您专业的朋友在那个领域。

代码:

URL url = new URL(detail.voucher_image.toString());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.getImageBitmap(bmp);

非常感谢。

I am making an application in android which loads Icon size images from URL

i have tried downloading images using the following code.

One image labeled default.png was downloaded from the given url but there was another image labeled v_1234.jpg is not being downloaded. I dont know whats the problem. it just returns me null for jpg image.

I am not sure that its a problem for .jpg format that my code is not downloading the jpg format images or Its the labeled name problem that due to Underscore (_) in the label makes it not downloadable..

Please help Friends you are professional in that field.

CODE:

URL url = new URL(detail.voucher_image.toString());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.getImageBitmap(bmp);

Thanks alot.

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

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

发布评论

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

评论(2

一城柳絮吹成雪 2024-12-02 08:28:07

试试这个代码

URLConnection conn = url.openConnection();
conn.setDoOutput(true);
InputStream reader;
reader=conn.getInputStream();
System.out.println("Compressed2!!!"+conn.getContentLength());
                int available = reader.available();

                int i=0;
                int count=0;
                int cc=0;

                while(reader.read()!=-1){
                    cc++;
                }
                System.out.println("available"+cc);
                data2 = new byte[cc];
                while ((i = reader.read(data2, count, data2.length-count)) != -1) {
                    count +=i;
                   cc++;
                }


                System.out.println("Compressed3!!!");
               // reader.read(data2,0,cc);
                System.out.println("Compressed!!!");
               // printBytes(data1,data2,"after");

                System.out.println("length b4!!!"+data2);


                System.out.println("data::"+new String(data2));
                System.out.println("The length is "+data2.length);
                bmp2=BitmapFactory.decodeByteArray(data2, 0, data2.length);
                if(bmp2==null)
                    System.out.println("The bitmap value is null");
                iv.setImageBitmap(bmp2);undefined

try this code

URLConnection conn = url.openConnection();
conn.setDoOutput(true);
InputStream reader;
reader=conn.getInputStream();
System.out.println("Compressed2!!!"+conn.getContentLength());
                int available = reader.available();

                int i=0;
                int count=0;
                int cc=0;

                while(reader.read()!=-1){
                    cc++;
                }
                System.out.println("available"+cc);
                data2 = new byte[cc];
                while ((i = reader.read(data2, count, data2.length-count)) != -1) {
                    count +=i;
                   cc++;
                }


                System.out.println("Compressed3!!!");
               // reader.read(data2,0,cc);
                System.out.println("Compressed!!!");
               // printBytes(data1,data2,"after");

                System.out.println("length b4!!!"+data2);


                System.out.println("data::"+new String(data2));
                System.out.println("The length is "+data2.length);
                bmp2=BitmapFactory.decodeByteArray(data2, 0, data2.length);
                if(bmp2==null)
                    System.out.println("The bitmap value is null");
                iv.setImageBitmap(bmp2);undefined
白鸥掠海 2024-12-02 08:28:07

使用以下代码从 url 获取位图

public Bitmap imageConvert(String url){
URL aURL = null;
Bitmap bm = null;
try {
final String imageUrl =imgstr.replaceAll(" ","%20");
Log.e("Image Url",imageUrl);
aURL = new URL(imageUrl);
URLConnection conn = aURL.openConnection();
InputStream is = conn.getInputStream(); 
BufferedInputStream bis = new BufferedInputStream(is); 
bm = BitmapFactory.decodeStream(new PatchInputStream(is)); 
is.close(); 
} 
catch (Exception e) {
Log.e("ProPic Exception",e.getMessage());
}
return bm;
}

use the following code to get bitmap from url

public Bitmap imageConvert(String url){
URL aURL = null;
Bitmap bm = null;
try {
final String imageUrl =imgstr.replaceAll(" ","%20");
Log.e("Image Url",imageUrl);
aURL = new URL(imageUrl);
URLConnection conn = aURL.openConnection();
InputStream is = conn.getInputStream(); 
BufferedInputStream bis = new BufferedInputStream(is); 
bm = BitmapFactory.decodeStream(new PatchInputStream(is)); 
is.close(); 
} 
catch (Exception e) {
Log.e("ProPic Exception",e.getMessage());
}
return bm;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文