从没有异步任务的JSON android加载位图

时间:2017-08-11 07:55:48

标签: android json bitmap android-asynctask

我想要求Android,我有JSON,我想将其加载到网格视图或列表视图中。我已经将我的JSON(base64)转换为Bitmap并使用AsyncTask加载它,但它确实很慢,因为我首先加载所有JSON然后显示它。实际上我不想先加载它然后显示所有JSON。我想逐个加载它。所以我不等太久。 我能用什么?

这里是我的示例代码;

 private class GetImageProduct extends AsyncTask<String,Void,Object>{

        @Override
        protected Objects doInBackground(String... params) {
            String productCode = params[0];
            String skuCode = params[1];
            String productImageUrl = ""+urlProduct+"?filter={\"where\":{\"Code\":\""+ productCode +"\"}}";
            HttpHandler sh = new HttpHandler();
            String jsonString = sh.makeServiceCall(productImageUrl);
            Log.e(TAG, "Response from url: " + jsonString);

            if(jsonString != null){
                try{
                    JSONArray productDetails = new JSONArray(jsonString);
                    for(int j=0 ;j<productDetails.length();j++){

                        JSONObject q = productDetails.getJSONObject(j);
                        String productImage = q.getString("Image");
                        String productName = q.getString("Name");
                        String encodeImage = productImage.replace("data:image/jpeg;base64,","").replace("data:image/png;base64,","");
                        byte[] decodedString = Base64.decode(encodeImage,Base64.DEFAULT);
                        Bitmap base64Bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
                        Bitmap resized = Bitmap.createScaledBitmap(base64Bitmap, 1000,1000,true);

                        HashMap<String, Object> productData = new HashMap<String,Object>();
                        productData.put("base64Bitmap",resized );
                        productData.put("productName",productName);
                        imageList.add(productData);


                    }
                }catch(final JSONException e){
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    LENGTH_LONG)
                                    .show();
                        }
                    });
                }
            }

            return null;
        }
        @Override
        protected void onPostExecute(Object result) {
            super.onPostExecute(result);
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }


            ExtendedSimpleAdapterMapping adapter2 = new ExtendedSimpleAdapterMapping(
                    MappingProductActivity.this, imageList,
                    R.layout.list_selected_product,
                    new String[]{"base64Bitmap","productName"},
                    new int[]{R.id.imageSelId, R.id.txtProductName}
            );
            lv.setAdapter(adapter2);
        }
    }

1 个答案:

答案 0 :(得分:1)

这不是一个好方法,您只需要从服务器返回imagename,并将其与您的网址http://localhost/images/yourimage.jpg

连接起来

用于将图像从Android发送到PHP并将其存储在服务器检查here

中的文件中