我从服务器获取base 64编码格式的图像字符串:,
。所以我下载并使用该图像字符串将其转换为位图。
现在我唯一的问题是,我必须在for循环中调用loadImage(base64Image)方法近八次。但是由于异步排序它没有单次调用方法,它直接进入volley get响应。 / p>
因此我无法将值保存到arraylist。这就是我得到arrayIndexOutOfBoundException的原因。
下面我发布了与此问题相关的logcat和代码:
Logcat:(已编辑)
hNiCHLy9Ax3gKMv81IBJ1457175735
CardFragment.java:
03-22 08:39:43.795 30971-30971/com.android.burblr E/urlAva: http://example.com/api/v1/file=3UulRIaeyVTpKYfkA5og1458204300
03-22 08:39:43.795 30971-30971/com.android.burblr E/base64Image: 3UulRIaeyVTpKYfkA5og1458204300
03-22 08:39:43.795 30971-30971/com.android.burblr E/Calling BitMAp....: 3UulRIaeyVTpKYfkA5og1458204300
03-22 08:39:43.795 30971-30971/com.android.burblr E/Calling try: Calling try
03-22 08:39:43.795 30971-30971/com.android.burblr E/getAvaArrStr: cUZdHWN9KzYMtmusvqnE1446770662
03-22 08:39:43.795 30971-30971/com.android.burblr E/urlAva: http://example.com/api/v1/file=cUZdHWN9KzYMtmusvqnE1446770662
03-22 08:39:43.795 30971-30971/com.android.burblr E/base64Image: cUZdHWN9KzYMtmusvqnE1446770662
03-22 08:39:43.795 30971-30971/com.android.burblr E/Calling BitMAp....: cUZdHWN9KzYMtmusvqnE1446770662
03-22 08:39:43.795 30971-30971/com.android.burblr E/Calling try: Calling try
03-22 08:39:43.805 30971-30971/com.android.burblr D/Volley: [1] DiskBasedCache.remove: Could not delete cache entry for key=http://example.com/api/v1/file=cUZdHWN9KzYMtmusvqnE1446770662, filename=993968043-2098536571
03-22 08:39:43.827 30971-31011/com.android.burblr W/EGL_emulation: eglSurfaceAttrib not implemented
03-22 08:39:43.827 30971-31011/com.android.burblr W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe195f3c0, error=EGL_SUCCESS
03-22 08:39:44.088 30971-30971/com.android.burblr E/came: came
03-22 08:39:44.088 30971-30971/com.android.burblr E/arrBitMap: []
我需要在调用 void hitImageApi(){
final ProgressDialog dialog = ProgressDialog.show(getActivity(), null, null);
ProgressBar spinner = new android.widget.ProgressBar( getActivity(),null,android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#009689"), android.graphics.PorterDuff.Mode.SRC_IN);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(spinner);
dialog.setCancelable(false);
dialog.show();
for (int i=0; i < alAvaArr.size(); i++){
getAvaArrStr = alAvaArr.get(i);
Log.e("getAvaArrStr",""+getAvaArrStr);
urlAva = BurblrUtils.BR_AVATAR_IMAGE + "file=" +getAvaArrStr ;
Log.e("urlAva", urlAva);
base64Image = getAvaArrStr;
Log.e("base64Image", "" + base64Image);
loadImage(base64Image);
requestAva = new StringRequest(Request.Method.GET, urlAva, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response != null && !response.startsWith("<HTML>")){
Log.e("ResponseAvatar", response);
dialog.dismiss();
try {
Toast.makeText(getActivity(), "Running ", Toast.LENGTH_SHORT).show();
getSwipeImage();
myAppAdapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
dialog.dismiss();
}
}else{
dialog.dismiss();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(error != null){
Log.e("error", error.toString());
dialog.dismiss();
}
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("file", getAvaArrStr );
Log.e("paramsImg", ""+params);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
}
RequestQueue queue = Volley.newRequestQueue(getActivity());
queue.add(requestAva);
queue.getCache().remove(urlAva);
}
void loadImage(String src) {
Log.e("Calling BitMAp....", "" + src);
try {
Log.e("Calling try", "Calling try");
URL url = new URL(src);
Log.e("url", ""+url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("myBitmap", ""+myBitmap);
bitMapToString(myBitmap);
} catch (IOException e) {
// Log exception
Log.e("IOError", ""+e);
}
}
public String bitMapToString(Bitmap bitmap){
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
Log.e("temp", ""+temp);
arrBitMap.add(temp);
return temp;
}
方法八次后调用volley GET响应。任何建议
答案 0 :(得分:1)
所以基本上你想避免你在onResponse上做的所有工作,直到加载图像被调用的次数等于alAvaArr数组的大小。因此,您可以向上声明一个全局int变量,并在onResponse中每次增加它。现在检查响应该变量的值是否等于alAvaArr数组的大小。如果没有,那么无论你在OnResponse中做什么都不做其他事情:
dialog.dismiss();
try {
Toast.makeText(getActivity(), "Running ", Toast.LENGTH_SHORT).show();
getSwipeImage();
myAppAdapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
dialog.dismiss();
}