fix bitmapHashMap

This commit is contained in:
Aran Zaiger 2016-07-08 21:29:26 +03:00
parent f72c82c3e9
commit e72bbb3fcf
2 changed files with 35 additions and 29 deletions

View file

@ -31,6 +31,7 @@ import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition; import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.LatLng;
@ -316,18 +317,6 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback {
}; };
private View.OnClickListener cameraBtnListener = new View.OnClickListener()
{
public void onClick(View v)
{
Log.d(TAG, "in camera Button");
}
};
private class getMarkersFromNotes extends AsyncTask<List<Note>, MarkerNoteStruct, Void> { private class getMarkersFromNotes extends AsyncTask<List<Note>, MarkerNoteStruct, Void> {
GoogleMap mMap; GoogleMap mMap;
@ -349,18 +338,21 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback {
@Override @Override
protected Void doInBackground(List<Note>... listOfNotes) { protected Void doInBackground(List<Note>... listOfNotes) {
Log.d(TAG, "in async BG"); Log.d(TAG, "in async BG");
BitmapDescriptor b;
for (Note n : listOfNotes[0]) { for (Note n : listOfNotes[0]) {
b = BitmapDescriptorFactory.fromBitmap(Bitmap.createScaledBitmap(Utils.getBitmapFromURL(n.getAvatar()), 80, 80, false));
MarkerOptions mo = new MarkerOptions() MarkerOptions mo = new MarkerOptions()
.title(n.getTitle()) .title(n.getTitle())
.position(new LatLng(n.getLat(), n.getLon())) .position(new LatLng(n.getLat(), n.getLon()))
.snippet(n.getBody()) .snippet(n.getBody())
.icon(BitmapDescriptorFactory.fromBitmap(Bitmap.createScaledBitmap(Utils.getBitmapFromURL(n.getAvatar()), 80, 80, false))); .icon(b);
publishProgress(new MarkerNoteStruct(n,mo)); publishProgress(new MarkerNoteStruct(n,mo));
} }
return null; return null;
// return markerOptionList;
} }

View file

@ -10,6 +10,8 @@ import android.widget.ImageView;
import com.android.volley.Response; import com.android.volley.Response;
import com.android.volley.VolleyError; import com.android.volley.VolleyError;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -21,6 +23,7 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
/** /**
* Created by JERLocal on 7/7/2016. * Created by JERLocal on 7/7/2016.
@ -30,22 +33,31 @@ public class Utils {
public static final String TAG = "Utils"; public static final String TAG = "Utils";
public static final String BASE_URL = "http://thesocialnotework-api.appspot.com/api"; public static final String BASE_URL = "http://thesocialnotework-api.appspot.com/api";
public static ProgressDialog progress; public static ProgressDialog progress;
private static HashMap<String, Bitmap> bitmapHash = new HashMap<>();
public static Bitmap getBitmapFromURL(String imageUrl) {
try { public static Bitmap getBitmapFromURL(String url) {
URL url = new URL(imageUrl); if (Utils.bitmapHash.containsKey(url)){
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); Log.d(TAG, "getBitmapFromURL: Found is hash");
connection.setDoInput(true); return bitmapHash.get(url);
connection.connect(); } else {
InputStream input = connection.getInputStream(); Log.d(TAG, "getBitmapFromURL: New value to HashMap");
Bitmap myBitmap = BitmapFactory.decodeStream(input); try {
return myBitmap; URL new_url = new URL(url);
} catch (IOException e) { HttpURLConnection connection = (HttpURLConnection) new_url.openConnection();
e.printStackTrace(); connection.setDoInput(true);
return null; connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
bitmapHash.put(url, myBitmap);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
} }
} }
@ -130,8 +142,10 @@ public class Utils {
@Override @Override
protected Bitmap doInBackground(Void... v) { protected Bitmap doInBackground(Void... v) {
Bitmap b = Utils.getBitmapFromURL(url); // Bitmap b;
return b;
return Utils.getBitmapFromURL(url);
} }
@Override @Override