001.
package
com.avonline.imageg;
002.
003.
import
java.io.BufferedInputStream;
004.
import
java.io.BufferedOutputStream;
005.
import
java.io.BufferedReader;
006.
import
java.io.ByteArrayOutputStream;
007.
import
java.io.Closeable;
008.
import
java.io.IOException;
009.
import
java.io.InputStream;
010.
import
java.io.InputStreamReader;
011.
import
java.io.OutputStream;
012.
import
java.net.URL;
013.
import
java.util.ArrayList;
014.
import
java.util.HashMap;
015.
016.
import
org.apache.http.HttpEntity;
017.
import
org.apache.http.HttpResponse;
018.
import
org.apache.http.StatusLine;
019.
import
org.apache.http.client.ClientProtocolException;
020.
import
org.apache.http.client.HttpClient;
021.
import
org.apache.http.client.methods.HttpGet;
022.
import
org.apache.http.impl.client.DefaultHttpClient;
023.
import
org.json.JSONArray;
024.
import
org.json.JSONException;
025.
import
org.json.JSONObject;
026.
027.
import
android.app.Activity;
028.
import
android.app.AlertDialog;
029.
import
android.content.Context;
030.
import
android.content.DialogInterface;
031.
import
android.content.DialogInterface.OnClickListener;
032.
import
android.content.Intent;
033.
import
android.graphics.Bitmap;
034.
import
android.graphics.BitmapFactory;
035.
import
android.net.Uri;
036.
import
android.os.AsyncTask;
037.
import
android.os.Bundle;
038.
import
android.util.Log;
039.
import
android.view.LayoutInflater;
040.
import
android.view.Menu;
041.
import
android.view.View;
042.
import
android.view.ViewGroup;
043.
import
android.view.Window;
044.
import
android.widget.AdapterView;
045.
import
android.widget.AdapterView.OnItemClickListener;
046.
import
android.widget.BaseAdapter;
047.
import
android.widget.Button;
048.
import
android.widget.GridView;
049.
import
android.widget.ImageView;
050.
import
android.widget.TextView;
051.
052.
053.
public
class
MainActivity
extends
Activity {
054.
055.
private
GridView gridV;
056.
private
ImageAdapter imageAdapter;
057.
058.
public
int
currentPage =
1
;
059.
public
int
TotalPage =
0
;
060.
061.
public
Button btnNext;
062.
public
Button btnPre;
063.
064.
ArrayList<HashMap<String, Object>> MyArrList =
new
ArrayList<HashMap<String, Object>>();
065.
066.
@Override
067.
public
void
onCreate(Bundle savedInstanceState) {
068.
super
.onCreate(savedInstanceState);
069.
070.
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
071.
072.
setContentView(R.layout.activity_main);
073.
074.
075.
gridV = (GridView) findViewById(R.id.gridView1);
076.
gridV.setClipToPadding(
false
);
077.
imageAdapter =
new
ImageAdapter(getApplicationContext());
078.
gridV.setAdapter(imageAdapter);
079.
080.
081.
082.
btnNext = (Button) findViewById(R.id.btnNext);
083.
084.
btnNext.setOnClickListener(
new
View.OnClickListener() {
085.
public
void
onClick(View v) {
086.
currentPage = currentPage +
1
;
087.
ShowData();
088.
}
089.
});
090.
091.
092.
btnPre = (Button) findViewById(R.id.btnPre);
093.
094.
btnPre.setOnClickListener(
new
View.OnClickListener() {
095.
public
void
onClick(View v) {
096.
currentPage = currentPage -
1
;
097.
ShowData();
098.
}
099.
});
100.
101.
102.
ShowData();
103.
104.
}
105.
106.
public
void
ShowData()
107.
{
108.
btnNext.setEnabled(
false
);
109.
btnPre.setEnabled(
false
);
110.
111.
setProgressBarIndeterminateVisibility(
true
);
112.
new
LoadContentFromServer().execute();
113.
}
114.
115.
116.
class
LoadContentFromServer
extends
AsyncTask<Object, Integer, Object> {
117.
118.
@Override
119.
protected
Object doInBackground(Object... params) {
120.
122.
123.
JSONArray data;
124.
try
{
125.
data =
new
JSONArray(getJSONUrl(url));
126.
127.
MyArrList =
new
ArrayList<HashMap<String, Object>>();
128.
HashMap<String, Object> map;
129.
130.
131.
132.
133.
134.
135.
int
displayPerPage =
20
;
136.
int
TotalRows = data.length();
137.
int
indexRowStart = ((displayPerPage*currentPage)-displayPerPage);
138.
139.
if
(TotalRows<=displayPerPage)
140.
{
141.
TotalPage =
1
;
142.
}
143.
else
if
((TotalRows % displayPerPage)==
0
)
144.
{
145.
TotalPage =(TotalRows/displayPerPage) ;
146.
}
147.
else
148.
{
149.
TotalPage =(TotalRows/displayPerPage)+
1
;
150.
TotalPage = (
int
)TotalPage;
151.
}
152.
int
indexRowEnd = displayPerPage * currentPage;
153.
if
(indexRowEnd > TotalRows)
154.
{
155.
indexRowEnd = TotalRows;
156.
}
157.
158.
gridV.setOnItemClickListener(
new
OnItemClickListener() {
159.
public
void
onItemClick(AdapterView<?> parent, View v,
160.
int
position,
long
id) {
161.
162.
String url = (String) MyArrList.get(position).get(
"url"
);
163.
Uri Urls1 = Uri.parse(url);
164.
Intent intent =
new
Intent(android.content.Intent.ACTION_VIEW);
165.
intent.setDataAndType(Urls1,
"video/mp4"
);
166.
startActivity(intent);
167.
}
168.
});
169.
170.
for
(
int
i = indexRowStart; i < indexRowEnd; i++){
171.
JSONObject c = data.getJSONObject(i);
172.
map =
new
HashMap<String, Object>();
173.
map.put(
"ImageID"
, (String)c.getString(
"ImageID"
));
174.
map.put(
"ItemID"
, (String)c.getString(
"ItemID"
));
175.
map.put(
"views"
, (String)c.getString(
"views"
));
176.
map.put(
"url"
, (String)c.getString(
"url"
));
177.
178.
179.
map.put(
"ImagePath"
, (String)c.getString(
"ImagePath"
));
180.
Bitmap newBitmap = loadBitmap(c.getString(
"ImagePath"
));
181.
map.put(
"ImagePathBitmap"
, newBitmap);
182.
183.
MyArrList.add(map);
184.
185.
publishProgress(i);
186.
187.
}
188.
189.
190.
}
catch
(JSONException e) {
191.
192.
e.printStackTrace();
193.
}
194.
195.
return
null
;
196.
}
197.
198.
@Override
199.
public
void
onProgressUpdate(Integer... progress) {
200.
imageAdapter.notifyDataSetChanged();
201.
}
202.
203.
@Override
204.
protected
void
onPostExecute(Object result) {
205.
206.
207.
if
(currentPage >= TotalPage)
208.
{
209.
btnNext.setEnabled(
false
);
210.
}
211.
else
212.
{
213.
btnNext.setEnabled(
true
);
214.
}
215.
216.
217.
if
(currentPage <=
1
)
218.
{
219.
btnPre.setEnabled(
false
);
220.
}
221.
else
222.
{
223.
btnPre.setEnabled(
true
);
224.
}
225.
226.
setProgressBarIndeterminateVisibility(
false
);
227.
}
228.
}
229.
230.
231.
class
ImageAdapter
extends
BaseAdapter {
232.
233.
private
Context mContext;
234.
235.
public
ImageAdapter(Context context) {
236.
mContext = context;
237.
}
238.
239.
public
int
getCount() {
240.
return
MyArrList.size();
241.
}
242.
243.
public
Object getItem(
int
position) {
244.
return
MyArrList.get(position);
245.
}
246.
247.
public
long
getItemId(
int
position) {
248.
return
position;
249.
}
250.
251.
public
View getView(
int
position, View convertView, ViewGroup parent) {
252.
253.
254.
LayoutInflater inflater = (LayoutInflater) mContext
255.
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
256.
257.
258.
if
(convertView ==
null
) {
259.
convertView = inflater.inflate(R.layout.activity_column,
null
);
260.
}
261.
262.
263.
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColPhoto);
264.
imageView.getLayoutParams().height =
150
;
265.
imageView.getLayoutParams().width =
250
;
266.
imageView.setPadding(
5
,
5
,
5
,
5
);
267.
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
268.
try
269.
{
270.
imageView.setImageBitmap((Bitmap)MyArrList.get(position).get(
"ImagePathBitmap"
));
271.
}
catch
(Exception e) {
272.
273.
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
274.
}
275.
276.
277.
TextView txtImageID = (TextView) convertView.findViewById(R.id.ColImageID);
278.
txtImageID.setPadding(
5
,
0
,
0
,
0
);
279.
txtImageID.setText(
""
+ MyArrList.get(position).get(
"ImageID"
).toString());
280.
281.
282.
TextView txtItemID = (TextView) convertView.findViewById(R.id.ColItemID);
283.
txtItemID.setPadding(
5
,
0
,
0
,
0
);
284.
txtItemID.setText(
""
+ MyArrList.get(position).get(
"views"
).toString());
285.
286.
return
convertView;
287.
288.
}
289.
290.
}
291.
292.
293.
public
String getJSONUrl(String url) {
294.
StringBuilder str =
new
StringBuilder();
295.
HttpClient client =
new
DefaultHttpClient();
296.
HttpGet httpGet =
new
HttpGet(url);
297.
try
{
298.
HttpResponse response = client.execute(httpGet);
299.
StatusLine statusLine = response.getStatusLine();
300.
int
statusCode = statusLine.getStatusCode();
301.
if
(statusCode ==
200
) {
302.
HttpEntity entity = response.getEntity();
303.
InputStream content = entity.getContent();
304.
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(content));
305.
String line;
306.
while
((line = reader.readLine()) !=
null
) {
307.
str.append(line);
308.
}
309.
}
else
{
310.
Log.e(
"Log"
,
"Failed to download file.."
);
311.
}
312.
}
catch
(ClientProtocolException e) {
313.
e.printStackTrace();
314.
}
catch
(IOException e) {
315.
e.printStackTrace();
316.
}
317.
return
str.toString();
318.
}
319.
320.
321.
private
static
final
String TAG =
"Image"
;
322.
private
static
final
int
IO_BUFFER_SIZE =
4
*
1024
;
323.
public
static
Bitmap loadBitmap(String url) {
324.
Bitmap bitmap =
null
;
325.
InputStream in =
null
;
326.
BufferedOutputStream out =
null
;
327.
328.
try
{
329.
in =
new
BufferedInputStream(
new
URL(url).openStream(), IO_BUFFER_SIZE);
330.
331.
final
ByteArrayOutputStream dataStream =
new
ByteArrayOutputStream();
332.
out =
new
BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
333.
copy(in, out);
334.
out.flush();
335.
336.
final
byte
[] data = dataStream.toByteArray();
337.
BitmapFactory.Options options =
new
BitmapFactory.Options();
338.
339.
340.
bitmap = BitmapFactory.decodeByteArray(data,
0
, data.length,options);
341.
}
catch
(IOException e) {
342.
Log.e(TAG,
"Could not load Bitmap from: "
+ url);
343.
}
finally
{
344.
closeStream(in);
345.
closeStream(out);
346.
}
347.
348.
return
bitmap;
349.
}
350.
351.
private
static
void
closeStream(Closeable stream) {
352.
if
(stream !=
null
) {
353.
try
{
354.
stream.close();
355.
}
catch
(IOException e) {
356.
android.util.Log.e(TAG,
"Could not close stream"
, e);
357.
}
358.
}
359.
}
360.
361.
private
static
void
copy(InputStream in, OutputStream out)
throws
IOException {
362.
byte
[] b =
new
byte
[IO_BUFFER_SIZE];
363.
int
read;
364.
while
((read = in.read(b)) != -
1
) {
365.
out.write(b,
0
, read);
366.
}
367.
}
368.
369.
@Override
370.
public
boolean
onCreateOptionsMenu(Menu menu) {
371.
372.
373.
menu.add(
0
,
0
,
0
, R.string.refersh);
374.
return
super
.onCreateOptionsMenu(menu);
375.
376.
}
377.
@Override
378.
public
void
onBackPressed() {
379.
AlertDialog.Builder qDlg =
new
AlertDialog.Builder(
this
);
380.
qDlg.setTitle(getResources().getString(R.string.quit));
381.
qDlg.setPositiveButton(getResources().getString(R.string.no),
null
);
382.
qDlg.setNegativeButton(getResources().getString(R.string.yes),
new
OnClickListener() {
383.
public
void
onClick(DialogInterface arg0,
int
arg1) {
384.
385.
finish();
386.
}
387.
});
388.
qDlg.show();
389.
}
390.
}