android– 如何将图像URL转换为Drawable Int

android– 如何将图像URL转换为Drawable Int,第1张

概述大家早上好!!!!我想在Imageview中显示图像.但我的要求是,我需要以DrawableIntegerFormat显示图像.我从服务器获取数据并以url格式获取图像.Forex:http://www.mymartmycart.com/images/detailed/3/4G_CONNECT_M1.jpeg我想以Drawable格式转换此图像.这是我的代码这是我的界面

大家早上好 !!!!

我想在ImagevIEw中显示图像.但我的要求是,我需要以Drawable Integer Format显示图像.我从服务器获取数据并以url格式获取图像.

@H_419_6@For ex :http://www.mymartmycart.com/images/detailed/3/4G_CONNECT_M1.jpeg

我想以Drawable格式转换此图像.

这是我的代码

这是我的界面

@H_419_6@ public interface ECCardData<T> { @DrawableRes Integer getMainBackgroundResource(); @DrawableRes Integer getheadBackgroundResource(); List<T> getListItems();}

我的得主和塞特

@H_419_6@ private Integer headBackgroundResource;public Integer getheadBackgroundResource() { return headBackgroundResource; } public voID setheadBackgroundResource(Integer headBackgroundResource) { this.headBackgroundResource = headBackgroundResource; }

这个代码我在哪里设置图像

@H_419_6@Integer drawableRes = dataset.get(position).getheadBackgroundResource(); if (drawableRes != null) { headVIEw.setheadImageBitmap(BitmapFactory.decodeResource(pagerContainer.getResources(), drawableRes, new BitmapFactoryOptions())); }

这是我的解析

@H_419_6@ public static ArrayList<CardData> ParseCraft(String response) throws JsONException { ArrayList<CardData> alUser = new ArrayList<>(); JsONObject JsonRoot = new JsONObject(response); JsONArray parentArray = JsonRoot.getJsONArray("products"); for (int j = 0; j < parentArray.length(); j++) { JsONObject finalObject = parentArray.getJsONObject(j); CardData user = new CardData(); user.setheadTitle(finalObject.getString("product")); user.setPersonname(finalObject.getString("product_code")); JsONObject productJsonObject = finalObject.getJsONObject("main_pair"); JsONObject productJsonObject1 = productJsonObject.getJsONObject("detailed"); user.setheadBackgroundResource(productJsonObject1.getString("image_path")); alUser.add(user); } return alUser; }

我的Json回应

@H_419_6@{"products": [ { "product_ID": "863", "product": "LAVA 4G CONNECT M1", "company_name": "SAPTHAGIRI MOBILES", "age_verification": "N", "age_limit": "0", "product_code": "SGMM00044", "product_type": "P", "status": "A", "company_ID": "34", "approved": "Y", "List_price": "0.00", "amount": "2", "weight": "0.000", "length": "0", "wIDth": "0", "height": "0", "shipPing_freight": "0.00", "low_avail_limit": "0", "timestamp": "1492758588", "updated_timestamp": "1500273558", "usergroup_IDs": "0", "is_edp": "N", "edp_shipPing": "N", "unlimited_download": "N", "tracking": "B", "free_shipPing": "N", "zero_price_action": "R", "is_pbp": "N", "is_op": "N", "is_oper": "N", "is_returnable": "Y", "return_period": "10", "avail_since": "0", "out_of_stock_actions": "N", "localization": "", "min_qty": "0", "max_qty": "0", "qty_step": "0", "List_qty_count": "0", "tax_IDs": "", "options_type": "P", "exceptions_type": "F", "details_layout": "default", "shipPing_params": "a:5:{s:16:\"min_items_in_Box\";i:0;s:16:\"max_items_in_Box\";i:0;s:10:\"Box_length\";i:0;s:9:\"Box_wIDth\";i:0;s:10:\"Box_height\";i:0;}", "facebook_obj_type": "activity", "buy_Now_url": "", "cod": "N", "price": "3094.000000", "category_IDs": [ 295 ], "position": "0", "SEO_name": "lava-4g-connect-m1", "SEO_path": "166/234/295", "average_rating": null, "discussion_type": "D", "discussion_thread_ID": "619", "main_category": 295, "main_pair": { "pair_ID": "2577", "image_ID": "0", "detailed_ID": "3266", "position": "0", "detailed": { "object_ID": "863", "object_type": "product", "image_path": "http://www.mymartmycart.com/images/detailed/3/4G_CONNECT_M1.jpeg", "alt": "", "image_x": "635", "image_y": "476", "http_image_path": "http://www.mymartmycart.com/images/detailed/3/4G_CONNECT_M1.jpeg", "https_image_path": "https://www.mymartmycart.com/images/detailed/3/4G_CONNECT_M1.jpeg", "absolute_path": "/home/mymartmycart/public_HTML/images/detailed/3/4G_CONNECT_M1.jpeg", "relative_path": "detailed/3/4G_CONNECT_M1.jpeg" } }, "base_price": "3094.000000", "selected_options": [], "has_options": false, "product_options": [], "discounts": { "A": 0, "P": 0 }, "product_features": [], "qty_content": [] }

解决方法:

这段代码对我来说是将Server image url转换为Bitmap

@H_419_6@ String drawableRes="http://kartpay.biz/API/v1/file/banner/IHMOcSHU7yoTVOkwYi1bOOz8shrXXrJhayCPFO17.jpeg" StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); try { URL url = new URL(drawableRes); headVIEw.setheadImageBitmap(BitmapFactory.decodeStream((inputStream)url.getContent())); } catch (IOException e) { //Log.e(TAG, e.getMessage()); } 总结

以上是内存溢出为你收集整理的android – 如何将图像URL转换为Drawable Int全部内容,希望文章能够帮你解决android – 如何将图像URL转换为Drawable Int所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1118466.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存