我是Android开发和Java的新手。我意识到这个问题可能看起来很简单,但我只是不理解,尽管在许多论坛上查看,阅读异常并调试我的活动。
我正在尝试将JSON图像数据解析为GridView。
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class JSONImageViewer extends Activity {
JSONObject jsonobject;
JSONArray jsonarray;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String TAG_IMG = "CarImageLink";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the view from gridview_item.xml
setContentView(R.layout.gridview_item);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(JSONImageViewer.this);
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
//Create an array
arraylist = new ArrayList<HashMap<String, String>>();
//Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("......");
try {
//Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("car_images");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
//Retrieve JSON Object
map.put("CarImageLink", jsonobject.getString("CarImageLink"));
//Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
//Locate the Gridview in gridview_main.xml
GridView gridview;
gridview = (GridView) findViewById(R.id.gridview);
adapter = new ListViewAdapter(JSONImageViewer.this, arraylist);
//Set the adapter to the GridView
gridview.setAdapter(adapter);
//Close the progressdialog
mProgressDialog.dismiss();
}
}
public static class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
//Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
//Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
public class ListViewAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context, ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView carimg;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.gridview_item, parent, false);
//Get the position
resultp = data.get(position);
//resultp.put(TAG_IMG,data.get(position).get("CarImageLink"));
//resultp.put(TAG_IMG,data.get(position).get("CarImageLink))"));
Log.v("resultp", resultp.toString());
//Locate the ImageView in gridview_item.xml
carimg = (ImageView) itemView.findViewById(R.id.car_img);
//Capture position and set results to the ImageView
//Passes images URL into ImageLoader.class
int loader = R.drawable.temp_img;
int stub_id = loader;
imageLoader.DisplayImage(resultp.get(JSONImageViewer.TAG_IMG),stub_id, carimg);
Log.v("resultp e1", (resultp.get(TAG_IMG).toString()));
//Log.v("carimg", carimg.toString());
Log.v("resultp e2", resultp.toString());
//Capture ListView item click
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Get the position
resultp = data.get(position);
//resultp.put(TAG_IMG,data.get(position).get("CarImageLink"));
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data
intent.putExtra("CarImageLink", resultp.get(JSONImageViewer.TAG_IMG));
//Start SingleItemView Class
context.startActivity(intent);
}
});
return itemView;
}
}
public class ImageLoader {
MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
// Handler to display images in UI thread
Handler handler = new Handler();
public ImageLoader(Context context){
fileCache=new FileCache(context);
executorService=Executors.newFixedThreadPool(5);
}
int stub_id = R.drawable.temp_img;
public void DisplayImage(String url, int loader, ImageView imageView)
{
try {
stub_id = loader;
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
if(bitmap!=null) {
imageView.setImageBitmap(bitmap);
Log.v("Bitmap 3", bitmap.toString());
}
else
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
Bitmap b = decodeFile(f);
if (b != null) {
Log.v("bitmap 4", b.toString());
return b;
}
//Download Images from the Internet
try {
Bitmap bitmap;
// Log.v("Bitmap 5", bitmap.toString());
Uri.Builder uri = Uri.parse("....").buildUpon();
uri.appendPath(url);
uri.build();
Log.v("Build", uri.build().toString());
Log.v("Uri", uri.toString());
URL imageUrl = new URL ("http://"+ "....com" + url);
Log.v("URL 3", imageUrl.toString());
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
conn.disconnect();
bitmap = decodeFile(f);
//Log.v("bitmap 6", bitmap.toString());
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
//Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream stream1 = new FileInputStream(f);
BitmapFactory.decodeStream(stream1, null, o);
stream1.close();
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
FileInputStream stream2 = new FileInputStream(f);
Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
stream2.close();
Log.v("bitmap 7", bitmap.toString());
return bitmap;
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
@Override
public void run() {
try {
//if (imageViewReused(photoToLoad))
// return;
Bitmap bmp = getBitmap(photoToLoad.url);
Log.v("URL 2", photoToLoad.url.toString());
Log.v("Bitmap bmp", bmp.toString());
memoryCache.put(photoToLoad.url, bmp);
//if (imageViewReused(photoToLoad))
//return;
//BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
//handler.post(bd);
//Log.v("bd", bd.toString());
} catch (Throwable th) {
th.printStackTrace();
}
}
}
/* boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
*/
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap=b;
Log.v("bitmap b", b.toString());
photoToLoad=p;
}
public void run(){
// if(imageViewReused(photoToLoad)) {
//Log.v("BITMAP", bitmap.toString());
// return;
//}
Log.v("BITMAP", bitmap.toString());
if(bitmap != null) {
photoToLoad.imageView.setImageBitmap(bitmap);
Log.v("bitmap 2", bitmap.toString());
}
else {
photoToLoad.imageView.setImageResource(stub_id);
}
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
public class MemoryCache {
private static final String TAG = "MemoryCache";
//Last argument true for LRU ordering
private Map<String, Bitmap> cache = Collections
.synchronizedMap(new LinkedHashMap<String, Bitmap>(10, 1.5f, true));
//Current allocated size
private long size = 0;
//Max memory in bytes
private long limit = 1000000;
public MemoryCache() {
//Use 25% of available heap size
setLimit(Runtime.getRuntime().maxMemory() / 4);
}
public void setLimit(long new_limit) {
limit = new_limit;
}
public Bitmap get(String id) {
try {
if (!cache.containsKey(id))
return null;
return cache.get(id);
} catch (NullPointerException ex) {
ex.printStackTrace();
return null;
}
}
public void put(String id, Bitmap bitmap) {
try {
if (cache.containsKey(id))
size -= getSizeInBytes(cache.get(id));
cache.put(id, bitmap);
size += getSizeInBytes(bitmap);
checkSize();
} catch (Throwable th) {
th.printStackTrace();
}
}
private void checkSize() {
Log.i(TAG, "cache size=" + size + " length=" + cache.size());
if (size > limit) {
//Least recently accessed item will be the first one iterated
Iterator<Map.Entry<String, Bitmap>> iter = cache.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Bitmap> entry = iter.next();
size -= getSizeInBytes(entry.getValue());
iter.remove();
if (size <= limit)
break;
}
Log.i(TAG, "Clean cache. New size " + cache.size());
}
}
public void clear() {
try {
cache.clear();
size = 0;
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
long getSizeInBytes(Bitmap bitmap) {
if (bitmap == null)
return 0;
return bitmap.getRowBytes() * bitmap.getHeight();
}
}
public class FileCache {
private File cacheDir;
public FileCache(Context context) {
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory(),
"");
else
cacheDir = context.getCacheDir();
if (!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url) {
String filename = String.valueOf(url.hashCode());
//String filename = URLEncoder.encode(url);
File f = new File(cacheDir, filename);
return f;
}
public void clear() {
File[] files = cacheDir.listFiles();
if (files == null)
return;
for (File f : files)
f.delete();
}
}
public static class Utils {
public static void CopyStream(InputStream is, OutputStream os)
{
final int buffer_size=1024;
try
{
byte[] bytes=new byte[buffer_size];
for(;;)
{
int count=is.read(bytes, 0, buffer_size);
if(count==-1)
break;
os.write(bytes, 0, count);
}
}
catch(Exception ex){}
}
}
public class SingleItemView extends Activity {
String carimg;
ImageLoader imageLoader = new ImageLoader(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the view from singleitemview.xml
setContentView(R.layout.singleitemview);
Intent i = getIntent();
//Get the result of CarImageLink
carimg = i.getStringExtra("CarImageLink");
Log.v("carimg", i.getStringExtra("CarImageLink").toString());
int loader = R.drawable.temp_img;
int stub_id = loader;
//Locate the ImageView in singleitemview.xml
ImageView img_car = (ImageView) findViewById(R.id.car_img);
Log.v("Imageview img_car", img_car.toString());
//Capture position and set results to the ImageView
//Passes carimg images URL into ImageLoader.class
imageLoader.DisplayImage(carimg, stub_id, img_car);
}
}
griview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cars"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/car_id"
android:text="@string/id"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/car_vin"
android:layout_below="@id/car_id"
android:text="@string/vin"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/model_img"
android:layout_toRightOf="@id/car_id"/>
<ListView
android:id="@+id/list"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
singleitemview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/car_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000000"
android:padding="1dp"
android:src="@drawable/temp_img" />
</RelativeLayout>
如何解决这些错误的任何建议将不胜感激。谢谢。
更新
java.lang.NullPointerException
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at com.example.justin.myapplication.JSONImageViewer$ImageLoader.DisplayImage(JSONImageViewer.java:290)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at com.example.justin.myapplication.JSONImageViewer$ListViewAdapter.getView(JSONImageViewer.java:229)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.widget.AbsListView.obtainView(AbsListView.java:2143)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.widget.GridView.makeAndAddView(GridView.java:1341)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.widget.GridView.makeRow(GridView.java:341)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.widget.GridView.fillDown(GridView.java:283)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.widget.GridView.fillFromTop(GridView.java:417)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.widget.GridView.layoutChildren(GridView.java:1229)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.widget.AbsListView.onLayout(AbsListView.java:1994)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.view.View.layout(View.java:14008)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.view.View.layout(View.java:14008)
07-30 15:30:54.609 29426-29426/com.example.justin.myapplication W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4373)
imageView.setImageResource(stub_id); for(JSONImageViewer.java:290) imageLoader.DisplayImage(resultp.get(JSONImageViewer.TAG_IMG),stub_id,carimg); for(JSONImageViewer.java:229)
答案 0 :(得分:1)
在Android中直接调试内容。
面对看似难以理解的异常堆栈跟踪,诀窍是扫描一下例程列表,查找您的代码中第一次出现的源代码行。在该行上设置断点,并查看实际发生的情况。您还应该寻找&#34;由&#34;这样做的内部异常。如果存在内部异常,您将在堆栈跟踪中找到内部异常的问题。
在您的情况下,感兴趣的行是堆栈跟踪中的第一行:
at com.example.justin.myapplication.JSONImageViewer$ImageLoader.DisplayImage(JSONImageViewer.java:287)
在第287行的JSONImageViewer.java中发生了Null指针异常。
在调试器中的该行上设置断点,并查看发生了什么。您只需双击堆栈跟踪中的文件和行号即可直接进入源代码行。
我无法分辨第287行是哪一行。但这似乎与您正在构建的网址有关。一旦在287行设置了断点,你应该能够很容易地解决这个问题。
答案 1 :(得分:1)
我认为这个问题是由于hashmap引起的。 散列映射由键和值组成。但是,您没有将键保存到指定的值。 (&#34; resultp = data.get(position);&#34;)。
因此,当您尝试使用键访问hashmap的值时:&#34; TAG_IMG&#34;,应用程序将使用空指针崩溃,因为这将返回空值。
解决此问题的正确方法是:resultp.put(TAG_IMG,data.get(position));
public class JSONImageViewer extends Activity {
JSONObject jsonobject;
JSONArray jsonarray;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String TAG_IMG = "CarImageLink";
GridView gridview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridview);
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(JSONImageViewer.this);
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
//Create an array
arraylist = new ArrayList<HashMap<String, String>>();
//Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("......");
try {
//Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("car_images");
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
//Retrieve JSON Object
map.put("CarImageLink" + i, jsonobject.getString("CarImageLink"));
//Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
if (arraylist != null && arraylist.size() > 0) {
adapter = new ListViewAdapter(getApplicationContext(), arraylist);
//Set the adapter to the GridView
gridview.setAdapter(adapter);
} else Toast.makeText(getApplicationContext(), "List is null", Toast.LENGTH_SHORT);
//Close the progressdialog
mProgressDialog.dismiss();
}
}
public static class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
//Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
//Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
public class ListViewAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context, ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
this.data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView carimg;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.gridview_item, parent, false);
//Get the position
resultp = data.get(position);
//resultp.put(TAG_IMG,data.get(position).get("CarImageLink"));
//resultp.put(TAG_IMG,data.get(position).get("CarImageLink))"));
Log.v("resultp", resultp.toString());
//Locate the ImageView in gridview_item.xml
carimg = (ImageView) itemView.findViewById(R.id.car_img);
//Capture position and set results to the ImageView
//Passes images URL into ImageLoader.class
int loader = R.drawable.temp_img;
int stub_id = loader;
if (carimg == null) {
Toast.makeText(getApplicationContext(), "A crash is going to happen due to error in xml", Toast.LENGTH_SHORT);
}
if (resultp.get(TAG_IMG) == null) {
Toast.makeText(getApplicationContext(), "A crash is going to happen due to hashma error", Toast.LENGTH_SHORT).show();
}
imageLoader.DisplayImage(resultp.get(TAG_IMG) + position, stub_id, carimg);
Log.v("resultp e1", (resultp.get(TAG_IMG).toString()));
//Log.v("carimg", carimg.toString());
Log.v("resultp e2", resultp.toString());
//Capture ListView item click
/*itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Get the position
resultp = data.get(position);
//resultp.put(TAG_IMG,data.get(position).get("CarImageLink"));
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data
intent.putExtra("CarImageLink", resultp.get(JSONImageViewer.TAG_IMG));
//Start SingleItemView Class
context.startActivity(intent);
}
});*/
return itemView;
}
}
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
// Handler to display images in UI thread
Handler handler = new Handler();
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}
int stub_id = R.drawable.temp_img;
public void DisplayImage(String url, int loader, ImageView imageView) {
try {
stub_id = loader;
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
Log.v("Bitmap 3", bitmap.toString());
} else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
Bitmap b = decodeFile(f);
if (b != null) {
Log.v("bitmap 4", b.toString());
return b;
}
//Download Images from the Internet
try {
Bitmap bitmap;
// Log.v("Bitmap 5", bitmap.toString());
Uri.Builder uri = Uri.parse("....").buildUpon();
uri.appendPath(url);
uri.build();
Log.v("Build", uri.build().toString());
Log.v("Uri", uri.toString());
URL imageUrl = new URL("http://" + "....com" + url);
Log.v("URL 3", imageUrl.toString());
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
conn.disconnect();
bitmap = decodeFile(f);
//Log.v("bitmap 6", bitmap.toString());
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
//Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream stream1 = new FileInputStream(f);
BitmapFactory.decodeStream(stream1, null, o);
stream1.close();
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
FileInputStream stream2 = new FileInputStream(f);
Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
stream2.close();
Log.v("bitmap 7", bitmap.toString());
return bitmap;
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
@Override
public void run() {
try {
//if (imageViewReused(photoToLoad))
// return;
Bitmap bmp = getBitmap(photoToLoad.url);
Log.v("URL 2", photoToLoad.url.toString());
Log.v("Bitmap bmp", bmp.toString());
memoryCache.put(photoToLoad.url, bmp);
//if (imageViewReused(photoToLoad))
//return;
//BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
//handler.post(bd);
//Log.v("bd", bd.toString());
} catch (Throwable th) {
th.printStackTrace();
}
}
}
/* boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
*/
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
Log.v("bitmap b", b.toString());
photoToLoad = p;
}
public void run() {
// if(imageViewReused(photoToLoad)) {
//Log.v("BITMAP", bitmap.toString());
// return;
//}
Log.v("BITMAP", bitmap.toString());
if (bitmap != null) {
photoToLoad.imageView.setImageBitmap(bitmap);
Log.v("bitmap 2", bitmap.toString());
} else {
photoToLoad.imageView.setImageResource(stub_id);
}
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
public class MemoryCache {
private static final String TAG = "MemoryCache";
//Last argument true for LRU ordering
private Map<String, Bitmap> cache = Collections
.synchronizedMap(new LinkedHashMap<String, Bitmap>(10, 1.5f, true));
//Current allocated size
private long size = 0;
//Max memory in bytes
private long limit = 1000000;
public MemoryCache() {
//Use 25% of available heap size
setLimit(Runtime.getRuntime().maxMemory() / 4);
}
public void setLimit(long new_limit) {
limit = new_limit;
}
public Bitmap get(String id) {
try {
if (!cache.containsKey(id))
return null;
return cache.get(id);
} catch (NullPointerException ex) {
ex.printStackTrace();
return null;
}
}
public void put(String id, Bitmap bitmap) {
try {
if (cache.containsKey(id))
size -= getSizeInBytes(cache.get(id));
cache.put(id, bitmap);
size += getSizeInBytes(bitmap);
checkSize();
} catch (Throwable th) {
th.printStackTrace();
}
}
private void checkSize() {
Log.i(TAG, "cache size=" + size + " length=" + cache.size());
if (size > limit) {
//Least recently accessed item will be the first one iterated
Iterator<Map.Entry<String, Bitmap>> iter = cache.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Bitmap> entry = iter.next();
size -= getSizeInBytes(entry.getValue());
iter.remove();
if (size <= limit)
break;
}
Log.i(TAG, "Clean cache. New size " + cache.size());
}
}
public void clear() {
try {
cache.clear();
size = 0;
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
long getSizeInBytes(Bitmap bitmap) {
if (bitmap == null)
return 0;
return bitmap.getRowBytes() * bitmap.getHeight();
}
}
public class FileCache {
private File cacheDir;
public FileCache(Context context) {
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory(),
"");
else
cacheDir = context.getCacheDir();
if (!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url) {
String filename = String.valueOf(url.hashCode());
//String filename = URLEncoder.encode(url);
File f = new File(cacheDir, filename);
return f;
}
public void clear() {
File[] files = cacheDir.listFiles();
if (files == null)
return;
for (File f : files)
f.delete();
}
}
public static class Utils {
public static void CopyStream(InputStream is, OutputStream os) {
final int buffer_size = 1024;
try {
byte[] bytes = new byte[buffer_size];
for (; ; ) {
int count = is.read(bytes, 0, buffer_size);
if (count == -1)
break;
os.write(bytes, 0, count);
}
} catch (Exception ex) {
}
}
}
/* public class SingleItemView extends Activity {
String carimg;
ImageLoader imageLoader = new ImageLoader(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the view from singleitemview.xml
setContentView(R.layout.singleitemview);
Intent i = getIntent();
//Get the result of CarImageLink
carimg = i.getStringExtra("CarImageLink");
Log.v("carimg", i.getStringExtra("CarImageLink").toString());
int loader = R.drawable.temp_img;
int stub_id = loader;
//Locate the ImageView in singleitemview.xml
ImageView img_car = (ImageView) findViewById(R.id.car_img);
Log.v("Imageview img_car", img_car.toString());
//Capture position and set results to the ImageView
//Passes carimg images URL into ImageLoader.class
imageLoader.DisplayImage(carimg, stub_id, img_car);
}
}*/
}