簡體   English   中英

從Android中的URL加載圖像

[英]Load Image from URL in Android

我正在嘗試從以下URL加載圖像: https ://fangkarte.de: 8080/ fangfisch/ file/ files/ 592036af55dfffca0155e01fe10002f7

在Chrome / IE中,圖像將正確顯示。 當我嘗試在Android上加載圖像時,無法將圖像存儲為PNG文件。 每次圖像將被保存為文本文件,其中包含圖像為base64字符串。

我也嘗試了以下代碼,但codedByte為空:

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

有誰知道如何將URL中的圖像顯示為位圖,或者我更喜歡將圖像存儲為png?

使用Universal Image Loder從Url上傳圖像下載Universal Image Loder並添加到您的lib文件夾中,然后

在要加載圖像的位置實現以下代碼

ImageLoader imageLoader = ImageLoader.getInstance();

imageLoader.init(ImageLoaderConfiguration.createDefault(context));

DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
        .cacheOnDisc(true).resetViewBeforeLoading(true)
        .showImageForEmptyUri(R.drawable.noimage)
        .showImageOnFail(R.drawable.icon3)
        .showImageOnLoading(R.drawable.loading).build();

imageLoader.displayImage(url, imageview, options);

要處理圖像,請使用Picasso,Glide,Universal圖像加載器,Volley或Fresco

例如,在畢加索中,您將能夠加載圖像,將其存儲在內存緩存中並處理listviews / recyclerviews等中的取消操作...

Picasso.with(context).load(“ https://fangkarte.de:8080/fangfisch/file/files/592036af55dfffca0155e01fe10002f7 ”).placeholder(“ someimagetillyourimagedownloads).into(R.id.yourimageview);

畢加索: https : //github.com/square/picasso

滑行: https : //github.com/bumptech/glide

壁畫: https : //github.com/facebook/fresco (我在廣泛測試了除凌空以外的所有3個之后使用了它)

UIL: https//github.com/nostra13/Android-Universal-Image-Loader

使用上述所有庫都可以完成類似的操作。

我強烈建議不要嘗試自行處理圖像。 如果您是新手android開發人員,可能會變得一團糟。

您下載的圖像很大((860.000字節)),可以從中制作出位圖。 因此,BitmapFactory將返回null。

使用前按比例縮小圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM