Advertisement
Guest User

Untitled

a guest
Oct 24th, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.65 KB | None | 0 0
  1. package com.myapp;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.util.HashMap;
  12. import java.util.List;
  13.  
  14. import org.json.JSONObject;
  15.  
  16. import android.app.Activity;
  17. import android.content.Context;
  18. import android.content.Intent;
  19. import android.graphics.Bitmap;
  20. import android.graphics.BitmapFactory;
  21. import android.os.AsyncTask;
  22. import android.os.Bundle;
  23. import android.support.v4.util.LruCache;
  24. import android.util.Log;
  25. import android.view.LayoutInflater;
  26. import android.view.Menu;
  27. import android.view.View;
  28. import android.view.ViewGroup;
  29. import android.widget.AdapterView;
  30. import android.widget.AdapterView.OnItemClickListener;
  31. import android.widget.BaseAdapter;
  32. import android.widget.GridView;
  33. import android.widget.ImageView;
  34. import android.widget.SimpleAdapter;
  35.  
  36.  
  37. public class MainActivity extends Activity {
  38.  
  39. GridView gridView;
  40. private LruCache mMemoryCache;
  41. @Override
  42. public void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.activity_main);
  45.  
  46. // URL to the JSON data
  47. // String strUrl = "http://ta.wptrafficanalyzer.in/demo1/first.php/countries/";
  48. String strUrl = "http://192.168.10.104/adchara1/";
  49.  
  50. // Creating a new non-ui thread task to download json data
  51. DownloadTask downloadTask = new DownloadTask();
  52.  
  53. // Starting the download process
  54. downloadTask.execute(strUrl);
  55.  
  56. // Getting a reference to ListView of activity_main
  57. gridView = (GridView) findViewById(R.id.lv_countries);
  58.  
  59. gridView.setOnItemClickListener(new OnItemClickListener() {
  60.  
  61. public void onItemClick(AdapterView<?> parent, View v, int positon,
  62. long id) {
  63. // Toast.makeText(getApplicationContext(), "welcome to hello gridview", Toast.LENGTH_SHORT).show();
  64. HashMap<String, Object> hm = (HashMap<String, Object>) gridView.getAdapter().getItem(positon);
  65. String imgPath = (String) hm.get("photo"); //get downloaded image path
  66. Intent i = new Intent(MainActivity.this, DisplayActivity.class); //start new Intent to another Activity.
  67. i.putExtra("ClickedImagePath", imgPath ); //put image link in intent.
  68. startActivity(i);
  69. }
  70. });
  71.  
  72. }
  73.  
  74. /** A method to download json data from url */
  75. private String downloadUrl(String strUrl) throws IOException{
  76. String data = "";
  77. InputStream iStream = null;
  78. try{
  79. URL url = new URL(strUrl);
  80.  
  81. // Creating an http connection to communicate with url
  82. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  83.  
  84. // Connecting to url
  85. urlConnection.connect();
  86.  
  87. // Reading data from url
  88. iStream = urlConnection.getInputStream();
  89.  
  90. BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
  91. StringBuffer sb = new StringBuffer();
  92.  
  93. String line = "";
  94. while( ( line = br.readLine()) != null){
  95. sb.append(line);
  96. }
  97.  
  98. data = sb.toString();
  99.  
  100. br.close();
  101.  
  102. }catch(Exception e){
  103. Log.d("Exception while downloading url", e.toString());
  104. }finally{
  105. iStream.close();
  106. }
  107.  
  108. return data;
  109. }
  110.  
  111. /** AsyncTask to download json data */
  112. private class DownloadTask extends AsyncTask<String, Integer, String>{
  113. String data = null;
  114. @Override
  115. protected String doInBackground(String... url) {
  116. try{
  117. data = downloadUrl(url[0]);
  118. }catch(Exception e){
  119. Log.d("Background Task",e.toString());
  120. }
  121. return data;
  122. }
  123.  
  124. @Override
  125. protected void onPostExecute(String result) {
  126.  
  127. // The parsing of the xml data is done in a non-ui thread
  128. GridViewLoaderTask gridViewLoaderTask = new GridViewLoaderTask();
  129.  
  130. // Start parsing xml data
  131. gridViewLoaderTask.execute(result);
  132. }
  133. }
  134.  
  135. /** AsyncTask to parse json data and load ListView */
  136. private class GridViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{
  137.  
  138. JSONObject jObject;
  139. // Doing the parsing of xml data in a non-ui thread
  140. @Override
  141. protected SimpleAdapter doInBackground(String... strJson) {
  142. try{
  143. jObject = new JSONObject(strJson[0]);
  144. CountryJSONParser countryJsonParser = new CountryJSONParser();
  145. countryJsonParser.parse(jObject);
  146. }catch(Exception e){
  147. Log.d("JSON Exception1",e.toString());
  148. }
  149.  
  150. // Instantiating json parser class
  151. CountryJSONParser countryJsonParser = new CountryJSONParser();
  152.  
  153. // A list object to store the parsed countries list
  154. List<HashMap<String, Object>> countries = null;
  155.  
  156. try{
  157. // Getting the parsed data as a List construct
  158. countries = countryJsonParser.parse(jObject);
  159. }catch(Exception e){
  160. Log.d("Exception",e.toString());
  161. }
  162.  
  163. // Keys used in Hashmap
  164. String[] from = { "frame","photo"};
  165.  
  166.  
  167. // Ids of views in listview_layout
  168. int[] to = { R.id.iv_frame,R.id.iv_photo};
  169.  
  170. // Instantiating an adapter to store each items
  171. // R.layout.listview_layout defines the layout of each item
  172. SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.lv_layout, from, to);
  173.  
  174. return adapter;
  175.  
  176. }
  177.  
  178.  
  179.  
  180. /** Invoked by the Android on "doInBackground" is executed */
  181. @Override
  182. protected void onPostExecute(SimpleAdapter adapter) {
  183.  
  184. // Setting adapter for the listview
  185. gridView.setAdapter(adapter);
  186.  
  187. for(int i=0;i<adapter.getCount();i++){
  188. HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
  189. String frameUrl = (String) hm.get("frame_path");
  190. String imgUrl = (String) hm.get("photo_path");
  191. ImageLoaderTask imageLoaderTask = new ImageLoaderTask();
  192.  
  193. HashMap<String, Object> hmDownload = new HashMap<String, Object>();
  194. hm.put("frame_path", frameUrl);
  195. hm.put("photo_path",imgUrl);
  196. hm.put("position", i);
  197.  
  198. // Starting ImageLoaderTask to download and populate image in the listview
  199. imageLoaderTask.execute(hm);
  200. }
  201. }
  202. }
  203.  
  204.  
  205. /** AsyncTask to download and load an image in ListView */
  206. private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{
  207.  
  208. @Override
  209. protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {
  210.  
  211. InputStream iStream=null;
  212. String imgUrl;
  213. String frameUrl;
  214. imgUrl = (String) hm[0].get("photo_path");
  215. frameUrl = (String) hm[0].get("frame_path");
  216. int position = (Integer) hm[0].get("position");
  217.  
  218. URL url;
  219. URL urlFrame;
  220. try {
  221. url = new URL(imgUrl);
  222. urlFrame = new URL(frameUrl);
  223. // Creating an http connection to communicate with url
  224. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  225.  
  226. // Connecting to url
  227. urlConnection.connect();
  228.  
  229. // Reading data from url
  230. iStream = urlConnection.getInputStream();
  231.  
  232. // Getting Caching directory
  233. File cacheDirectory = getBaseContext().getCacheDir();
  234.  
  235. // Temporary file to store the downloaded image
  236. File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png");
  237.  
  238. // The FileOutputStream to the temporary file
  239. FileOutputStream fOutStream = new FileOutputStream(tmpFile);
  240.  
  241. // Creating a bitmap from the downloaded inputstream
  242.  
  243. Bitmap b = BitmapFactory.decodeStream(iStream);
  244.  
  245. // Writing the bitmap to the temporary file as png file
  246. b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
  247.  
  248. // Flush the FileOutputStream
  249. fOutStream.flush();
  250.  
  251. //Close the FileOutputStream
  252. fOutStream.close();
  253.  
  254. // Create a hashmap object to store image path and its position in the listview
  255. HashMap<String, Object> hmBitmap = new HashMap<String, Object>();
  256.  
  257. // Storing the path to the temporary image file
  258. hmBitmap.put("photo",tmpFile.getPath());
  259. hmBitmap.put("frame", tmpFile.getPath());
  260.  
  261. // Storing the position of the image in the listview
  262. hmBitmap.put("position",position);
  263.  
  264.  
  265. // Returning the HashMap object containing the image path and position
  266. return hmBitmap;
  267.  
  268. }catch (Exception e) {
  269. e.printStackTrace();
  270. }
  271. return null;
  272. }
  273.  
  274. @Override
  275. protected void onPostExecute(HashMap<String, Object> result) {
  276. // Getting the path to the downloaded image
  277. String path = (String) result.get("photo");
  278. String framePath = (String) result.get("frame");
  279. // Getting the position of the downloaded image
  280. int position = (Integer) result.get("position");
  281.  
  282. // Getting adapter of the listview
  283. SimpleAdapter adapter = (SimpleAdapter ) gridView.getAdapter();
  284.  
  285. // Getting the hashmap object at the specified position of the listview
  286. HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position);
  287.  
  288. // Overwriting the existing path in the adapter
  289. hm.put("photo",path);
  290. hm.put("frame", framePath);
  291. // Noticing listview about the dataset changes
  292. adapter.notifyDataSetChanged();
  293. }
  294. }
  295.  
  296. public static class ViewHolder{
  297. ImageView iv_photo;
  298. ImageView iv_frame;
  299. }
  300.  
  301. public class ImageAdapter extends BaseAdapter{
  302.  
  303. public int getCount() {
  304. // TODO Auto-generated method stub
  305. return 0;
  306. }
  307.  
  308. public Object getItem(int position) {
  309. // TODO Auto-generated method stub
  310. return null;
  311. }
  312.  
  313. public long getItemId(int position) {
  314. // TODO Auto-generated method stub
  315. return 0;
  316. }
  317.  
  318.  
  319. @Override
  320. public View getView(int position, View convertView, ViewGroup parent) {
  321. ViewHolder holder;
  322. View view = convertView;
  323. if(convertView == null){
  324. holder = new ViewHolder();
  325. LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  326. view = inflator.inflate(R.layout.lv_layout, parent, false);
  327.  
  328. holder.iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
  329. holder.iv_photo.setImageResource(R.drawable.ic_launcher);
  330. }
  331. return null;
  332. }
  333.  
  334. }
  335.  
  336. @Override
  337. public boolean onCreateOptionsMenu(Menu menu) {
  338. getMenuInflater().inflate(R.menu.activity_main, menu);
  339. return true;
  340. }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement